如何在 Python 中列出所有已安装的软件包及其版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12939975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list all installed packages and their versions in Python?
提问by jsalonen
Is there a way in Python to list all installed packages and their versions?
Python 中有没有办法列出所有已安装的软件包及其版本?
I know I can go inside python/Lib/site-packagesand see what files and directories exist, but I find this very awkward. What I'm looking for something that is similar to npm listi.e. npm-ls.
我知道我可以进去python/Lib/site-packages看看有哪些文件和目录存在,但我觉得这很尴尬。我正在寻找的东西类似于npm list即NPM-LS。
采纳答案by Jeff LaFay
If you have pip install and you want to see what packages have been installed with your installer tools you can simply call this:
如果你有 pip install 并且你想看看你的安装工具安装了哪些包,你可以简单地调用它:
pip freeze
It will also include version numbers for the installed packages.
它还将包括已安装软件包的版本号。
Update
更新
pip has been updated to also produce the same output as pip freezeby calling:
pip 已更新为也产生与pip freeze调用相同的输出:
pip list
Note
笔记
The output from pip listis formatted differently, so if you have some shell script that parses the output (maybe to grab the version number) of freezeand want to change your script to call list, you'll need to change your parsing code.
from 的输出pip list格式不同,因此如果您有一些 shell 脚本来解析 的输出(可能是为了获取版本号)freeze并希望将脚本更改为 call list,则需要更改解析代码。
回答by Ashwini Chaudhary
help('modules')should do it for you.
help('modules')应该为你做。
in IPython :
在 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
回答by Andbdrew
yes! you should be using pip as your python package manager ( http://pypi.python.org/pypi/pip)
是的!你应该使用 pip 作为你的 python 包管理器(http://pypi.python.org/pypi/pip)
with pip installed packages, you can do a
使用 pip 安装的软件包,你可以做一个
pip freeze
and it will list all installed packages. You should probably also be using virtualenvand virtualenvwrapper. When you start a new project, you can do
它将列出所有已安装的软件包。您可能还应该使用virtualenv和virtualenvwrapper。当你开始一个新项目时,你可以这样做
mkvirtualenv my_new_project
and then (inside that virtualenv), do
然后(在那个 virtualenv 内),做
pip install all_your_stuff
This way, you can workon my_new_projectand then pip freezeto see which packages are installed for that virtualenv/project.
这样,您可以workon my_new_project然后pip freeze查看为该 virtualenv/项目安装了哪些包。
for example:
例如:
? ~ 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)? ~
or if you have a python package with a requirements.pip file,
或者如果你有一个带有 requirements.pip 文件的 python 包,
mkvirtualenv my_awesome_project
pip install -r requirements.pip
pip freeze
will do the trick
会做的伎俩
回答by chown
Here's a way to do it using PYTHONPATHinstead of the absolute path of your python libs dir:
这是一种使用PYTHONPATHpython libs目录的绝对路径代替绝对路径的方法:
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
....
回答by llazzaro
You can try : Yolk
你可以试试:蛋黄
For install yolk, try:
要安装蛋黄,请尝试:
easy_install yolk
Yolk is a Python tool for obtaining information about installed Python packages and querying packages avilable on PyPI (Python Package Index).
You can see which packages are active, non-active or in development mode and show you which have newer versions available by querying PyPI.
Yolk 是一个 Python 工具,用于获取有关已安装 Python 包的信息并查询 PyPI(Python 包索引)上可用的包。
您可以查看哪些包处于活动状态、非活动状态或处于开发模式,并通过查询 PyPI 向您显示哪些具有较新版本可用。
回答by frosty
If you want to get information about your installed python distributions and don't want to use your cmd console or terminal for it, but rather through python code, you can use the following code (tested with python 3.4):
如果您想获取有关已安装的 python 发行版的信息,并且不想使用 cmd 控制台或终端,而是通过 python 代码,您可以使用以下代码(使用 python 3.4 测试):
import pip #needed to use the pip functions
for i in pip.get_installed_distributions(local_only=True):
print(i)
The pip.get_installed_distributions(local_only=True)function-call returns an iterable and because of the for-loop and the print function the elements contained in the iterable are printed out separated by new line characters (\n).
The result will (depending on your installed distributions) look something like this:
该pip.get_installed_distributions(local_only=True)函数调用返回一个迭代并且因为for循环和打印功能包含在所述元件的可迭代打印出来通过新行字符分隔(\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...
回答by Npradhan
from command line
从命令行
python -c help('modules')
can be used to view all modules, and for specific modules
可用于查看所有模块,以及针对特定模块
python -c help('os')
For Linux below will work
对于 Linux,下面将工作
python -c "help('os')"
回答by A. Bollans
If you're using anaconda:
如果您使用的是蟒蛇:
conda list
will do it! See: https://conda.io/docs/_downloads/conda-cheatsheet.pdf
会做的!请参阅:https: //conda.io/docs/_downloads/conda-cheatsheet.pdf
回答by exhuma
To run this in later versions of pip (tested on pip==10.0.1) use the following:
要在更高版本的 pip(已测试pip==10.0.1)中运行它,请使用以下命令:
from pip._internal.operations.freeze import freeze
for requirement in freeze(local_only=True):
print(requirement)
回答by alfredocambera
My take:
我的看法:
#!/usr/bin/env python3
import pkg_resources
dists = [str(d).replace(" ","==") for d in pkg_resources.working_set]
for i in dists:
print(i)

