Is there any Python script or Python code that can give me the list of Python projects (distribution packages: libraries, etc.) installed (with pip, for example) so that I can iterate over them in a loop?
It should be working in python3.x
.
Is there any Python script or Python code that can give me the list of Python projects (distribution packages: libraries, etc.) installed (with pip, for example) so that I can iterate over them in a loop?
It should be working in python3.x
.
There is a very simple and straightforward way to do this directly in Python's standard library. Use importlib.metadata
.
For example:
#!/usr/bin/env python3
import importlib.metadata
for distribution in importlib.metadata.distributions():
print(distribution.metadata['Name'], distribution.metadata['Version'])
This will work for Python 3.8+. For versions older than 3.8 I would recommend using importlib-metadata
to get the exact same features.
The pip list
command will give you a list of all packages and their versions installed:
Package Version
----------------------------- ------------
ailment 9.0.4663
alabaster 0.7.12
angr 9.0.4663
appdirs 1.4.4
apsw 3.33.0.post1
... more lines that I cut off
If you want to loop through this in python, then you can use subprocess.check_output
to capture the output then do a bit of string processing:
import subprocess
packages = subprocess.check_output(["python3", "-m", "pip", "list"]).decode("utf8")
package_lines = map(str.split, packages.splitlines()[2:])
for package_name, version in package_lines:
print(f"Package {package_name} has version {version}")
You can use
pip freeze
on your terminal to list all the installed python modules.
If you want those modules as a list in your python program, you would use pkg_resources module
# Credit: https://www.activestate.com/resources/quick-reads/how-to-list-installed-python-packages/
import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
You can easily do that using the pip library in python:
try:
from pip._internal.operations import freeze
except ImportError: # in case pip version < 10.0
from pip.operations import freeze
modules = list(freeze.freeze())
Now the modules variable is a list containing all the pip installed modules/packages.
here's just whats stored in modules for me when i ran it:
['altgraph==0.17', 'appdirs==1.4.4', 'argon2-cffi==20.1.0', 'async-generator==1.10', 'attrs==20.2.0', 'auto-py-to-exe==2.7.8', 'autopep8==1.5.4', 'backcall==0.2.0', 'beautifulsoup4==4.9.1', 'bleach==3.2.1', 'bottle==0.12.18', 'bottle-websocket==0.2.9', 'cachetools==4.1.1', 'certifi==2020.6.20', 'cffi==1.14.3', 'chardet==3.0.4', 'click==7.1.2', 'colorama==0.4.3', 'cryptography==3.2.1', 'cycler==0.10.0', 'decorator==4.4.2', 'defusedxml==0.6.0', 'distlib==0.3.1', 'docopt==0.6.2', 'Eel==0.12.4', 'entrypoints==0.3', 'et-xmlfile==1.0.1', and so on.....]
I have cut it short as there is alot for me.