0

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Aditya
  • 1,132
  • 1
  • 9
  • 28
  • I found this answer, I think this is your question Check this out: https://stackoverflow.com/questions/739993/how-can-i-get-a-list-of-locally-installed-python-modules – MOHAMED HABI Nov 21 '20 at 14:38
  • Does this answer your question? [How can I get a list of locally installed Python modules?](https://stackoverflow.com/questions/739993/how-can-i-get-a-list-of-locally-installed-python-modules) – Ivanhercaz Nov 21 '20 at 15:54

4 Answers4

3

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • Get more info in [Single-sourcing the package version](https://packaging.python.org/guides/single-sourcing-package-version/#single-sourcing-the-version) point **5.**. – plastique Sep 01 '21 at 07:05
0

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}")
Aplet123
  • 33,825
  • 1
  • 29
  • 55
0

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)
ar_n
  • 56
  • 3
-1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
cyberGhoul
  • 99
  • 2
  • 7
  • _pip_ is not meant to be used as a library (see [here for details](https://pip.pypa.io/en/stable/user_guide/#id9)). This is not a good recommendation. There are better ways to do this. using [_`importlib.metadata`_](https://docs.python.org/3/library/importlib.metadata.html) is the current best recommendation. – sinoroc Nov 21 '20 at 17:28