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


当前回答

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

import sys as s
s.modules.keys()

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

对于所有内置模块使用:

s.modules

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

其他回答

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

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

这会有所帮助

在终端或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。虽然它不会给你一个准确的列表,但你可以通过查看你的env>lib>python(版本在这里)>site-packages>来了解安装了哪些库。在这里,您可以很好地了解已安装的模块。

安装

pip install pkgutil

Code

import pkgutil

for i in pkgutil.iter_modules(None): # returns a tuple (path, package_name, ispkg_flag)
    print(i[1]) #or you can append it to a list

样例输出:

multiprocessing
netrc
nntplib
ntpath
nturl2path
numbers
opcode
pickle
pickletools
pipes
pkgutil

使用pkgutil.iter_modules进行非常简单的搜索

from pkgutil import iter_modules
a=iter_modules()
while True:
    try: x=a.next()
    except: break
    if 'searchstr' in x[1]: print x[1]