How can I get a list of locally installed Python m

2018-12-31 12:37发布

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?

22条回答
萌妹纸的霸气范
2楼-- · 2018-12-31 12:59

Since pip version 1.3, you've got access to:

pip list

Which seems to be syntactic sugar for "pip freeze". It will list all of the modules particular to your installation or virtualenv, along with 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.

查看更多
柔情千种
3楼-- · 2018-12-31 13:00

I normally use pip list to get a list of packages (with version).

This works in a virtual environment too, of course.

查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 13:02

If you are using Python 3

I just tried on Ubuntu, and it seems to have worked

Debian / Ubuntu: sudo apt-get install python3-matplotlib

Fedora: sudo dnf install python3-matplotlib

Red Hat: sudo yum install python3-matplotlib

Arch: sudo pacman -S python-matplotlib

Source: https://matplotlib.org/users/installing.html

查看更多
柔情千种
5楼-- · 2018-12-31 13:03

I just use this to see currently used modules:

import sys as s
s.modules.keys()

which shows all modules running on your python.

For all built-in modules use:

s.modules

Which is a dict containing all modules and import objects.

查看更多
泛滥B
6楼-- · 2018-12-31 13:06
  1. to get all available modules, run sys.modules
  2. to get all installed modules (read: installed by pip), you may look at pip.get_installed_distributions()

For the second purpose, example code:

import pip
for package in pip.get_installed_distributions():
    name = package.project_name # SQLAlchemy, Django, Flask-OAuthlib
    key = package.key # sqlalchemy, django, flask-oauthlib
    module_name = package._get_metadata("top_level.txt") # sqlalchemy, django, flask_oauthlib
    location = package.location # virtualenv lib directory etc.
    version = package.version # version number
查看更多
ら面具成の殇う
7楼-- · 2018-12-31 13:07

As of pip 10, the accepted answer will no longer work. The development team has removed access to the get_installed_distributions routine. There is an alternate function in the setuptools for doing the same thing. Here is an alternate version that works with pip 10:

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)

Please let me know if it will or won't work in previous versions of pip, too.

查看更多
登录 后发表回答