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


当前回答

PIP冻结可以找到所有的包,但是你可以简单地写下面的命令来列出python包所在的所有路径。

>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

其他回答

对于任何想知道如何从Python程序调用pip list的人,您可以使用以下方法:

import pip
pip.main(['list])  # this will print all the packages

我只是用它来查看当前使用的模块:

import sys as s
s.modules.keys()

显示在python上运行的所有模块。

对于所有内置模块使用:

s.modules

它是一个包含所有模块和导入对象的字典。

在windows上,在cmd中输入这个

c:\python\libs>python -m pip freeze

这会有所帮助

在终端或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

从外壳开始

ls site-packages

如果没有帮助,你可以这样做。

import sys
import os
for p in sys.path:
    print os.listdir( p )

看看会产生什么。