How to list all installed packages and their versions in Python?
在python中有没有方法列出所有已安装的包及其版本?
我知道我可以进入
如果您安装了PIP,并且希望看到安装工具安装了哪些软件包,您可以简单地调用它:
1 | pip freeze |
它还将包括已安装软件包的版本号。
更新
通过调用以下命令,pip也被更新,以产生与
1 | pip list |
注释
来自
在IPython:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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控制台或终端,而是通过python代码,您可以使用以下代码(用python 3.4测试):
1 2 3 | import pip #needed to use the pip functions for i in pip.get_installed_distributions(local_only=True): print(i) |
1 2 3 4 5 6 7 8 9 10 11 | 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... |
你可以试试:蛋黄
要安装York,请尝试:
1 | 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.
从命令行
1 | python -c help('modules') |
可用于查看所有模块以及特定模块
1 | python -c help('os') |
对于Linux,下面的将起作用
1 | python -c"help('os')" |
对!您应该使用pip作为python包管理器(http://pypi.python.org/pypi/pip)
使用安装了PIP的软件包,您可以
1 | pip freeze |
它将列出所有已安装的软件包。您可能还应该使用virtualenv和virtualenwrapper。当你开始一个新项目时,你可以
1 | mkvirtualenv my_new_project |
然后(在virtualenv中),做
1 | pip install all_your_stuff |
这样,您可以先
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ? ~ 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包,
1 2 3 | mkvirtualenv my_awesome_project pip install -r requirements.pip pip freeze |
会成功的
要在更高版本的PIP(在
1 2 3 | from pip._internal.operations.freeze import freeze for requirement in freeze(local_only=True): print(requirement) |
下面是一种使用
1 2 | for d in `echo"${PYTHONPATH}" | tr ':' ' '`; do ls"${d}"; done |
1 2 3 4 5 6 7 8 9 | [ 10:43 Jonathan@MacBookPro-2 ~/xCode/Projects/Python for iOS/trunk/Python for iOS/Python for iOS ]$ for d in `echo"$PYTHONPATH" | tr ':' ' '`; 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 .... |
如果你用的是Python:
1 | conda list |
会的!参见:https://conda.io/docs//u downloads/conda-cridsheet.pdf
我的拿手:
1 2 3 4 5 6 7 | #!/usr/bin/env python3 import pkg_resources dists = [str(d).replace("","==") for d in pkg_resources.working_set] for i in dists: print(i) |