How do I get a list of locally installed Python modules?
You can use the pip module to get a list of locally installed Python modules.
You can use the importlib.metadata module to get a list of locally installed Python modules. Here is a code snippet that does this:
Get a list of locally installed Python modules
from importlib.metadata import distributions
module_list = sorted([dist.metadata['Name'] for dist in distributions()])
print(module_list)This code imports distributions from importlib.metadata, retrieves all installed packages, extracts their names, sorts the list alphabetically, and prints it. This approach uses the standard library and is compatible with Python 3.8+.
Note that this approach requires Python 3.8+ and avoids deprecated pip internal APIs that were removed in pip 20.0.