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


当前回答

从pip 10开始,接受的答案将不再有效。开发团队已经删除了对get_installed_distribution例程的访问。在setuptools中有一个替代函数可以做同样的事情。下面是一个适用于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)

请让我知道它是否会工作在以前版本的pip,太。

其他回答

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

import sys as s
s.modules.keys()

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

对于所有内置模块使用:

s.modules

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

这会有所帮助

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

使用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]

在ipython中,你可以输入“importTab”。 在标准的Python解释器中,您可以输入“help('modules')”。 在命令行中,您可以使用pydoc模块。 在脚本中,调用pkgutil.iter_modules()。

试试这些

pip list

or

pip freeze