如何获得在计算机上安装的Python模块列表?


当前回答

从pip 1.3版本开始,您可以访问:

pip list

这似乎是“pip freeze”的语法糖。它将列出所有特定于您的安装或virtualenv的模块,以及它们的版本号。不幸的是,它不显示任何模块的当前版本号,也不洗碗或擦鞋。

其他回答

解决方案

不要与pip > 10.0一起使用!

我从Python脚本中获得一个类似于pip的冻结列表的50美分:

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)

作为(太长的)一行:

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

给:

['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']

范围

该解决方案适用于系统作用域或虚拟环境作用域,涵盖了由setuptools、pip和easy_install(但愿不会)安装的包。

我的用例

我将这个调用的结果添加到我的flask服务器,所以当我用http://example.com/exampleServer/environment调用它时,我得到了服务器的virtualenv上安装的包的列表。它使调试变得更加容易。

警告

我注意到这种技术的一个奇怪行为——当Python解释器在与setup.py文件相同的目录中调用时,它不会列出setup.py安装的包。

复制步骤:

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.

我们在/tmp/behave中有下面的setup.py:

(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

如果我们从/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'

如果我们从/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,因为工作目录包含了Behave的setup.py文件。

我在文件中找不到任何关于这个问题的资料。也许我该为它打开一只虫子。

我在OS x上遇到了一个自定义安装的python 2.7。它需要X11来列出已安装的模块(使用help和pydoc)。

为了能够在不安装X11的情况下列出所有模块,我运行pydoc作为http-server,即:

pydoc -p 12345

然后,可以将Safari定向到http://localhost:12345/以查看所有模块。

我通常使用pip list来获取包的列表(带有版本)。

当然,这也适用于虚拟环境。要显示只在虚拟环境中安装了什么(而不是全局包),请使用pip list——local。

下面的文档显示了所有可用的pip列表选项,并提供了几个很好的示例。

这会有所帮助

在终端或IPython中输入:

help('modules')

then

In [1]: import                      #import press-TAB
Display all 631 possibilities? (y or n)
ANSI                   audiodev               markupbase
AptUrl                 audioop                markupsafe
ArgImagePlugin         avahi                  marshal
BaseHTTPServer         axi                    math
Bastion                base64                 md5
BdfFontFile            bdb                    mhlib
BmpImagePlugin         binascii               mimetools
BufrStubImagePlugin    binhex                 mimetypes
CDDB                   bisect                 mimify
CDROM                  bonobo                 mmap
CGIHTTPServer          brlapi                 mmkeys
Canvas                 bsddb                  modulefinder
CommandNotFound        butterfly              multifile
ConfigParser           bz2                    multiprocessing
ContainerIO            cPickle                musicbrainz2
Cookie                 cProfile               mutagen
Crypto                 cStringIO              mutex
CurImagePlugin         cairo                  mx
DLFCN                  calendar               netrc
DcxImagePlugin         cdrom                  new
Dialog                 cgi                    nis
DiscID                 cgitb                  nntplib
DistUpgrade            checkbox               ntpath

试试这些

pip list

or

pip freeze