如何在Python中列出所有已安装的软件包及其版本?

有没有在Python中列出所有安装的软件包及其版本?

我知道我可以进入python/Lib/site-packages ,看看有什么文件和目录存在,但我觉得这很尴尬。 我正在寻找类似于npm list [1]的东西 。

如果你已经安装了pip,而且你想看看你的安装工具已经安装了哪些软件包,你可以直接调用它:

 pip freeze 

它还将包括已安装软件包的版本号。

更新

点子已被更新,也产生了与pip freeze相同的输出通过调用:

 pip list 

注意

来自pip list的输出格式不同,所以如果你有一些shell脚本来parsingfreeze的输出(也许是获取版本号)并且想把你的脚本改成call list ,那么你需要改变你的parsing代码。

help('modules')应该为你做。

在IPython中:

 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 

如果你想获得有关你安装的Python分布的信息,而不想使用你的CMD控制台或terminal,而是通过Python代码,你可以使用下面的代码(用python 3.4testing):

 import pip #needed to use the pip functions for i in pip.get_installed_distributions(local_only=True): print(i) 

pip.get_installed_distributions(local_only=True)函数调用返回一个迭代,由于for循环和打印函数,iterable中包含的元素被打印出来,并以换行符( \n )分隔。 结果将(取决于您安装的分布)看起来像这样:

 cycler 0.9.0 decorator 4.0.4 ipykernel 4.1.0 ipython 4.0.0 ipython-genutils 0.1.0 ipywidgets 4.0.3 Jinja2 2.8 jsonschema 2.5.1 jupyter 1.0.0 jupyter-client 4.1.1 #... and so on... 

你可以尝试: 蛋黄

要安装蛋黄,请尝试:

 easy_install yolk 

Yolk是一个Python工具,用于获取有关已安装Python包的信息,以及在PyPI(Python Package Index)上查询可用的包。

您可以查看哪些软件包处于活动状态,非活动状态或处于开发模式,并通过查询PyPI来显示哪些软件包具有较新版本。

从命令行

 python -c help('modules') 

可以用来查看所有的模块和特定的模块

 python -c help('os') 

对于Linux下面的工作

 python -c "help('os')" 

是! 你应该使用Python作为你的Python包pipe理器( http://pypi.python.org/pypi/pip

用pip安装软件包,你可以做一个

 pip freeze 

它会列出所有已安装的软件包。 你也应该使用virtualenv和virtualenvwrapper 。 当你开始一个新的项目,你可以做

 mkvirtualenv my_new_project 

然后(在virtualenv里面)做

 pip install all_your_stuff 

这样,您可以在workon my_new_project ,然后使用pip freeze查看为该virtualenv /项目安装了哪些软件包。

例如:

 ➜ ~ mkvirtualenv yo_dude New python executable in yo_dude/bin/python Installing setuptools............done. Installing pip...............done. virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/preactivate virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/postactivate virtualenvwrapper.user_scripts creating /Users/aaylward/dev/virtualenvs/yo_dude/bin/get_env_details (yo_dude)➜ ~ pip install django Downloading/unpacking django Downloading Django-1.4.1.tar.gz (7.7Mb): 7.7Mb downloaded Running setup.py egg_info for package django Installing collected packages: django Running setup.py install for django changing mode of build/scripts-2.7/django-admin.py from 644 to 755 changing mode of /Users/aaylward/dev/virtualenvs/yo_dude/bin/django-admin.py to 755 Successfully installed django Cleaning up... (yo_dude)➜ ~ pip freeze Django==1.4.1 wsgiref==0.1.2 (yo_dude)➜ ~ 

或者如果你有一个带有requirements.pip文件的python包,

 mkvirtualenv my_awesome_project pip install -r requirements.pip pip freeze 

会做的伎俩

下面是使用PYTHONPATH而不是Python库的绝对path的方法:

 for d in `echo "${PYTHONPATH}" | tr ':' '\n'`; do ls "${d}"; done 

 [ 10:43 Jonathan@MacBookPro-2 ~/xCode/Projects/Python for iOS/trunk/Python for iOS/Python for iOS ]$ for d in `echo "$PYTHONPATH" | tr ':' '\n'`; do ls "${d}"; done libpython2.7.dylib pkgconfig python2.7 BaseHTTPServer.py _pyio.pyc cgitb.pyo doctest.pyo htmlentitydefs.pyc mimetools.pyc plat-mac runpy.py stringold.pyc traceback.pyo BaseHTTPServer.pyc _pyio.pyo chunk.py dumbdbm.py htmlentitydefs.pyo mimetools.pyo platform.py runpy.pyc stringold.pyo tty.py BaseHTTPServer.pyo _strptime.py chunk.pyc dumbdbm.pyc htmllib.py mimetypes.py platform.pyc runpy.pyo stringprep.py tty.pyc Bastion.py _strptime.pyc chunk.pyo dumbdbm.pyo htmllib.pyc mimetypes.pyc platform.pyo sched.py stringprep.pyc tty.pyo Bastion.pyc _strptime.pyo cmd.py ....