Coder Perfect

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

Problem

I’d want to receive a list of Python modules that are installed in my Python environment (UNIX server).

What is the best way to acquire a list of Python modules that are installed on your computer?

Asked by Léo Léopold Hertz 준영

Solution #1

help('modules')

in a Python shell/prompt.

Answered by ChristopheD

Solution #2

My $0.50 for obtaining a pip freeze-like list from a Python script is as follows:

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 (excessively 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']

This method applies to packages installed via setuptools, pip, and (heaven forbid) easy install, and it can be applied to a system or a virtual environment.

I saved the result of this call in my flask server, so now when I call it with http://example.com/exampleServer/environment, I get a list of packages installed on the virtualenv of the server. Debugging becomes a lot easier as a result.

When the Python interpreter is launched in the same directory as a setup.py file, it does not list the package installed by setup.py, which is a peculiar behavior.

$ cd /tmp
$ virtualenv test_env
New python executable in test_env/bin/python
Installing setuptools, pip...done.
$ source test_env/bin/activate
(test_env) $ 
(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.

In /tmp/behave, we have behave’s setup.py:

(test_env) $ ls /tmp/behave/setup.py
/tmp/behave/setup.py
(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
>>> 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'
>>> 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'

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

There was no mention of this problem in the manual. Perhaps I’ll file a bug report about it.

Answered by Adam Matan

Solution #3

Now, I tested these procedures for myself, and I received exactly what was promised: all of the modules.

Unfortunately, you don’t really care about the stdlib; you already know what you receive when you install Python.

I really want the equipment that I set up.

Surprisingly, what actually worked well was:

pip freeze

Which returned:

Fabric==0.9.3
apache-libcloud==0.4.0
bzr==2.3b4
distribute==0.6.14
docutils==0.7
greenlet==0.3.1
ipython==0.10.1
iterpipes==0.4
libxml2-python==2.6.21

I say “surprisingly” because the package install tool is the exact place one would expect to find this functionality, although not under the name ‘freeze’ but python packaging is so weird, that I am flabbergasted that this tool makes sense. Pip 0.8.2, Python 2.7.

Answered by chiggsy

Solution #4

You have access to the following features since pip version 1.3:

pip list

This appears to be syntactic sugar for the phrase “pip freeze.” It will display a list of all the modules specific to your installation or virtualenv, as well as their version numbers. Unfortunately it does not display the current version number of any module, nor does it wash your dishes or shine your shoes.

Answered by Bryce

Solution #5

Answered by Johnsyweb

Post is based on https://stackoverflow.com/questions/739993/how-can-i-get-a-list-of-locally-installed-python-modules