How can I get a list of locally installed Python modules?

Léo Léopold Hertz 준영

I would like to get a list of Python modules, which are in my Python installation (UNIX server).

How can you get a list of Python modules installed in your computer?

Adam Matan

Solution

Do not use with pip > 10.0!

My 50 cents for getting a pip freeze-like list from a Python script:

import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
print(installed_packages_list)

As a (too long) one liner:

sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])

Giving:

['behave==1.2.4', 'enum34==1.0', 'flask==0.10.1', 'itsdangerous==0.24', 
 'jinja2==2.7.2', 'jsonschema==2.3.0', 'markupsafe==0.23', 'nose==1.3.3', 
 'parse-type==0.3.4', 'parse==1.6.4', 'prettytable==0.7.2', 'requests==2.3.0',
 'six==1.6.1', 'vioozer-metadata==0.1', 'vioozer-users-server==0.1', 
 'werkzeug==0.9.4']

Scope

This solution applies to the system scope or to a virtual environment scope, and covers packages installed by setuptools, pip and (god forbid) easy_install.

My use case

I added the result of this call to my flask server, so when I call it with http://example.com/exampleServer/environment I get the list of packages installed on the server's virtualenv. It makes debugging a whole lot easier.

Caveats

I have noticed a strange behaviour of this technique - when the Python interpreter is invoked in the same directory as a setup.py file, it does not list the package installed by setup.py.

Steps to reproduce:

Create a virtual environment
$ cd /tmp
$ virtualenv test_env
New python executable in test_env/bin/python
Installing setuptools, pip...done.
$ source test_env/bin/activate
(test_env) $ 
Clone a git repo with setup.py
(test_env) $ git clone https://github.com/behave/behave.git
Cloning into 'behave'...
remote: Reusing existing pack: 4350, done.
remote: Total 4350 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4350/4350), 1.85 MiB | 418.00 KiB/s, done.
Resolving deltas: 100% (2388/2388), done.
Checking connectivity... done.

We have behave's setup.py in /tmp/behave:

(test_env) $ ls /tmp/behave/setup.py
/tmp/behave/setup.py
Install the python package from the git repo
(test_env) $ cd /tmp/behave && pip install . 
running install
...
Installed /private/tmp/test_env/lib/python2.7/site-packages/enum34-1.0-py2.7.egg
Finished processing dependencies for behave==1.2.5a1

If we run the aforementioned solution from /tmp

>>> import pip
>>> sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
['behave==1.2.5a1', 'enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']
>>> import os
>>> os.getcwd()
'/private/tmp'

If we run the aforementioned solution from /tmp/behave

>>> import pip
>>> sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
['enum34==1.0', 'parse-type==0.3.4', 'parse==1.6.4', 'six==1.6.1']
>>> import os
>>> os.getcwd()
'/private/tmp/behave'

behave==1.2.5a1 is missing from the second example, because the working directory contains behave's setup.py file.

I could not find any reference to this issue in the documentation. Perhaps I shall open a bug for it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get a list of all pre-installed server modules in PHP?

From Dev

Django can't find locally-installed Python modules

From Dev

How do I get a list of python libraries I've installed?

From Dev

How do I get a list of programs that can be installed?

From Dev

How can I get a list of the biggest packages currently installed in order?

From Dev

How can I set my path so that it works with Python code that is locally installed as a package

From Dev

How can I get a list of available modules in AngularJS?

From Dev

How can I get a list of available modules in AngularJS?

From Dev

How can I list content of installed gem?

From Dev

How can I list content of installed gem?

From Dev

How to list all modules python 2.7 and 3.4 have installed for MacOSX

From Dev

How can I get a list of all packages available for a specific version of Ubuntu (not necessarily the one I have installed)?

From Dev

How can I get the installed ALSA version?

From Dev

How can I get the installed ALSA version?

From Java

How to use executables from a package installed locally in node_modules?

From Dev

Python at Synology, how to get Python3 modules installed and where is Python2.7 installed?

From Dev

How do I get the list of third-party python packages that I have installed with pip?

From Dev

How can I get Unity to recognize installed applications it doesn't list?

From Dev

How can I get Unity to recognize installed applications it doesn't list?

From Dev

How can I get the list of the currently installed certificate authority on my ubuntu?

From Dev

How can I get a list of installed centos packages by only reading from the filesystem?

From Dev

How can I tell if Python setuptools is installed?

From Dev

Locally installed versus globally installed NPM modules

From Dev

Using Search API, can I get the list of installed apps on iOS?

From Dev

How can I import modules to Python in Processing?

From Dev

How can I import modules to Python in Processing?

From Dev

How can I install modules of python in Ubuntu?

From Dev

RPM-based Linux: How can I remove a locally installed package?

From Dev

Python can't see modules installed with pip

Related Related

  1. 1

    How can I get a list of all pre-installed server modules in PHP?

  2. 2

    Django can't find locally-installed Python modules

  3. 3

    How do I get a list of python libraries I've installed?

  4. 4

    How do I get a list of programs that can be installed?

  5. 5

    How can I get a list of the biggest packages currently installed in order?

  6. 6

    How can I set my path so that it works with Python code that is locally installed as a package

  7. 7

    How can I get a list of available modules in AngularJS?

  8. 8

    How can I get a list of available modules in AngularJS?

  9. 9

    How can I list content of installed gem?

  10. 10

    How can I list content of installed gem?

  11. 11

    How to list all modules python 2.7 and 3.4 have installed for MacOSX

  12. 12

    How can I get a list of all packages available for a specific version of Ubuntu (not necessarily the one I have installed)?

  13. 13

    How can I get the installed ALSA version?

  14. 14

    How can I get the installed ALSA version?

  15. 15

    How to use executables from a package installed locally in node_modules?

  16. 16

    Python at Synology, how to get Python3 modules installed and where is Python2.7 installed?

  17. 17

    How do I get the list of third-party python packages that I have installed with pip?

  18. 18

    How can I get Unity to recognize installed applications it doesn't list?

  19. 19

    How can I get Unity to recognize installed applications it doesn't list?

  20. 20

    How can I get the list of the currently installed certificate authority on my ubuntu?

  21. 21

    How can I get a list of installed centos packages by only reading from the filesystem?

  22. 22

    How can I tell if Python setuptools is installed?

  23. 23

    Locally installed versus globally installed NPM modules

  24. 24

    Using Search API, can I get the list of installed apps on iOS?

  25. 25

    How can I import modules to Python in Processing?

  26. 26

    How can I import modules to Python in Processing?

  27. 27

    How can I install modules of python in Ubuntu?

  28. 28

    RPM-based Linux: How can I remove a locally installed package?

  29. 29

    Python can't see modules installed with pip

HotTag

Archive