查找pip安装了哪个版本的软件包

问题:查找pip安装了哪个版本的软件包

使用pip,可以确定当前安装了哪个版本的软件包?

我知道,pip install XYZ --upgrade但是我想知道是否有类似的东西pip info XYZ。如果不是,最好的方法就是告诉我当前使用的版本。

Using pip, is it possible to figure out which version of a package is currently installed?

I know about pip install XYZ --upgrade but I am wondering if there is anything like pip info XYZ. If not what would be the best way to tell what version I am currently using.


回答 0

pip 1.3开始,有一个pip show命令。

$ pip show Jinja2
---
Name: Jinja2
Version: 2.7.3
Location: /path/to/virtualenv/lib/python2.7/site-packages
Requires: markupsafe

在旧版本,pip freezegrep应做的工作很好。

$ pip freeze | grep Jinja2
Jinja2==2.7.3

As of pip 1.3, there is a pip show command.

$ pip show Jinja2
---
Name: Jinja2
Version: 2.7.3
Location: /path/to/virtualenv/lib/python2.7/site-packages
Requires: markupsafe

In older versions, pip freeze and grep should do the job nicely.

$ pip freeze | grep Jinja2
Jinja2==2.7.3

回答 1

我刚刚以增强的雨果·塔瓦雷斯(Hugo Tavares)的点子发送了请求请求:

(以specloud为例)

$ pip show specloud

Package: specloud
Version: 0.4.4
Requires:
nose
figleaf
pinocchio

I just sent a pull request in pip with the enhancement Hugo Tavares said:

(specloud as example)

$ pip show specloud

Package: specloud
Version: 0.4.4
Requires:
nose
figleaf
pinocchio

回答 2

Pip 1.3现在也有一个list命令:

$ pip list
argparse (1.2.1)
pip (1.5.1)
setuptools (2.1)
wsgiref (0.1.2)

Pip 1.3 now also has a list command:

$ pip list
argparse (1.2.1)
pip (1.5.1)
setuptools (2.1)
wsgiref (0.1.2)

回答 3

并将–outdated作为额外的参数,您将获得所使用软件包的当前版本和最新版本:

$ pip list --outdated
distribute (Current: 0.6.34 Latest: 0.7.3)
django-bootstrap3 (Current: 1.1.0 Latest: 4.3.0)
Django (Current: 1.5.4 Latest: 1.6.4)
Jinja2 (Current: 2.6 Latest: 2.8)

因此,结合AdamKG的答案:

$ pip list --outdated | grep Jinja2
Jinja2 (Current: 2.6 Latest: 2.8)

也检查pip-toolshttps : //github.com/nvie/pip-tools

and with –outdated as an extra argument, you will get the Current and Latest versions of the packages you are using :

$ pip list --outdated
distribute (Current: 0.6.34 Latest: 0.7.3)
django-bootstrap3 (Current: 1.1.0 Latest: 4.3.0)
Django (Current: 1.5.4 Latest: 1.6.4)
Jinja2 (Current: 2.6 Latest: 2.8)

So combining with AdamKG ‘s answer :

$ pip list --outdated | grep Jinja2
Jinja2 (Current: 2.6 Latest: 2.8)

Check pip-tools too : https://github.com/nvie/pip-tools


回答 4

您还可以安装yolk然后运行yolk -l,这也会提供一些不错的输出。这是我的小virtualenv获得的:

(venv)CWD> /space/vhosts/pyramid.xcode.com/venv/build/unittest 
project@pyramid 43> yolk -l
Chameleon       - 2.8.2        - active 
Jinja2          - 2.6          - active 
Mako            - 0.7.0        - active 
MarkupSafe      - 0.15         - active 
PasteDeploy     - 1.5.0        - active 
Pygments        - 1.5          - active 
Python          - 2.7.3        - active development (/usr/lib/python2.7/lib-dynload)
SQLAlchemy      - 0.7.6        - active 
WebOb           - 1.2b3        - active 
account         - 0.0          - active development (/space/vhosts/pyramid.xcode.com/project/account)
distribute      - 0.6.19       - active 
egenix-mx-base  - 3.2.3        - active 
ipython         - 0.12         - active 
logilab-astng   - 0.23.1       - active 
logilab-common  - 0.57.1       - active 
nose            - 1.1.2        - active 
pbkdf2          - 1.3          - active 
pip             - 1.0.2        - active 
pyScss          - 1.1.3        - active 
pycrypto        - 2.5          - active 
pylint          - 0.25.1       - active 
pyramid-debugtoolbar - 1.0.1        - active 
pyramid-tm      - 0.4          - active 
pyramid         - 1.3          - active 
repoze.lru      - 0.5          - active 
simplejson      - 2.5.0        - active 
transaction     - 1.2.0        - active 
translationstring - 1.1          - active 
venusian        - 1.0a3        - active 
waitress        - 0.8.1        - active 
wsgiref         - 0.1.2        - active development (/usr/lib/python2.7)
yolk            - 0.4.3        - active 
zope.deprecation - 3.5.1        - active 
zope.interface  - 3.8.0        - active 
zope.sqlalchemy - 0.7          - active 

You can also install yolk and then run yolk -l which also gives some nice output. Here is what I get for my little virtualenv:

(venv)CWD> /space/vhosts/pyramid.xcode.com/venv/build/unittest 
project@pyramid 43> yolk -l
Chameleon       - 2.8.2        - active 
Jinja2          - 2.6          - active 
Mako            - 0.7.0        - active 
MarkupSafe      - 0.15         - active 
PasteDeploy     - 1.5.0        - active 
Pygments        - 1.5          - active 
Python          - 2.7.3        - active development (/usr/lib/python2.7/lib-dynload)
SQLAlchemy      - 0.7.6        - active 
WebOb           - 1.2b3        - active 
account         - 0.0          - active development (/space/vhosts/pyramid.xcode.com/project/account)
distribute      - 0.6.19       - active 
egenix-mx-base  - 3.2.3        - active 
ipython         - 0.12         - active 
logilab-astng   - 0.23.1       - active 
logilab-common  - 0.57.1       - active 
nose            - 1.1.2        - active 
pbkdf2          - 1.3          - active 
pip             - 1.0.2        - active 
pyScss          - 1.1.3        - active 
pycrypto        - 2.5          - active 
pylint          - 0.25.1       - active 
pyramid-debugtoolbar - 1.0.1        - active 
pyramid-tm      - 0.4          - active 
pyramid         - 1.3          - active 
repoze.lru      - 0.5          - active 
simplejson      - 2.5.0        - active 
transaction     - 1.2.0        - active 
translationstring - 1.1          - active 
venusian        - 1.0a3        - active 
waitress        - 0.8.1        - active 
wsgiref         - 0.1.2        - active development (/usr/lib/python2.7)
yolk            - 0.4.3        - active 
zope.deprecation - 3.5.1        - active 
zope.interface  - 3.8.0        - active 
zope.sqlalchemy - 0.7          - active 

回答 5

您可以使用grep命令进行查找。

pip show <package_name>|grep Version

例:

pip show urllib3|grep Version

将仅显示版本。

元数据版本:2.0
版本:1.12

You can use the grep command to find out.

pip show <package_name>|grep Version

Example:

pip show urllib3|grep Version

will show only the versions.

Metadata-Version: 2.0
Version: 1.12


回答 6

最简单的方法是这样的:

import jinja2
print jinja2.__version__

The easiest way is this:

import jinja2
print jinja2.__version__

回答 7

还有一个名为的工具pip-check,可为您提供所有已安装软件包及其更新状态的快速概述:

我自己没有使用过;只是偶然地偶然发现了这个问题,因此没有被提及…

There’s also a tool called pip-check which gives you a quick overview of all installed packages and their update status:

Haven’t used it myself; just stumbled upon it and this SO question in quick succession, and since it wasn’t mentioned…


回答 8

在Windows上,您可以发出以下命令:

pip show setuptools | findstr "Version"

输出:

Version: 34.1.1

On windows, you can issue command such as:

pip show setuptools | findstr "Version"

Output:

Version: 34.1.1

回答 9

python函数仅以机器可读格式返回软件包版本:

from importlib.metadata import version 
version('numpy')

在python 3.8之前:

pip install importlib-metadata 
from importlib_metadata import version
version('numpy')

bash等效项(这里也从python调用)会复杂得多(但更健壮-请参见下面的注意事项):

import subprocess
def get_installed_ver(pkg_name):
    bash_str="pip freeze | grep -w %s= | awk -F '==' {'print $2'} | tr -d '\n'" %(pkg_name)
    return(subprocess.check_output(bash_str, shell=True).decode())

用法示例:

# pkg_name="xgboost"
# pkg_name="Flask"
# pkg_name="Flask-Caching"
pkg_name="scikit-learn"

print(get_installed_ver(pkg_name))
>>> 0.22

请注意,在两种情况下,pkg_name参数都应包含程序包名称,其格式应为返回的格式,pip freeze而不是在此过程中使用的格式import,例如scikit-learnnot sklearnFlask-Caching,not flask_caching

请注意,虽然pip freeze在bash版本中调用似乎效率低下,但只有这种方法被证明足够强大,可以打包命名的特殊性和不一致性(例如,下划线vs破折号,小写vs大写字母以及缩写,例如sklearnvs scikit-learn)。

注意:在复杂的环境中,这两种变体都可能返回令人惊讶的版本号,这与您在期间实际获得的内容不一致import

用户 site-packages子文件夹中隐藏了其他版本的软件包时,就会出现这样的问题。为了说明使用version()这种情况的危险,我遇到了这种情况:

$ pip freeze | grep lightgbm
lightgbm==2.3.1

and

$ python -c "import lightgbm; print(lightgbm.__version__)"
2.3.1

vs.

$ python -c "from importlib_metadata import version; print(version(\"lightgbm\"))"
2.2.3

until you delete the subfolder with the old version (here 2.2.3) from the user folder (only one would normally be preserved by `pip` - the one installed as last with the `--user` switch):

$ ls /home/jovyan/.local/lib/python3.7/site-packages/lightgbm*
/home/jovyan/.local/lib/python3.7/site-packages/lightgbm-2.2.3.dist-info
/home/jovyan/.local/lib/python3.7/site-packages/lightgbm-2.3.1.dist-info

另一个问题是在同一环境中有一些conda安装的软件包。如果它们与pip安装的软件包共享依赖关系,并且这些依赖关系的版本不同,则您可能会降低pip安装的依赖关系的等级。

为了说明这一点,numpyPyPI于04-01-2020 上提供的最新版本是1.18.0,与此同时,Anaconda的conda-forge频道仅提供了1.17.3版本numpy。因此,当您basemap使用conda(第二个)安装软件包时,先前pip安装的numpy版本将被conda降级为1.17.3,并且该import功能无法使用1.18.0版本。在这种情况下version()是正确的,和pip freeze/或conda list错误的:

$ python -c "from importlib_metadata import version; print(version(\"numpy\"))"
1.17.3

$ python -c "import numpy; print(numpy.__version__)"
1.17.3

$ pip freeze | grep numpy
numpy==1.18.0

$ conda list | grep numpy
numpy                     1.18.0                   pypi_0    pypi

The python function returning just the package version in a machine-readable format:

from importlib.metadata import version 
version('numpy')

Prior to python 3.8:

pip install importlib-metadata 
from importlib_metadata import version
version('numpy')

The bash equivalent (here also invoked from python) would be much more complex (but more robust – see caution below):

import subprocess
def get_installed_ver(pkg_name):
    bash_str="pip freeze | grep -w %s= | awk -F '==' {'print $2'} | tr -d '\n'" %(pkg_name)
    return(subprocess.check_output(bash_str, shell=True).decode())

Sample usage:

# pkg_name="xgboost"
# pkg_name="Flask"
# pkg_name="Flask-Caching"
pkg_name="scikit-learn"

print(get_installed_ver(pkg_name))
>>> 0.22

Note that in both cases pkg_name parameter should contain package name in the format as returned by pip freeze and not as used during import, e.g. scikit-learn not sklearn or Flask-Caching, not flask_caching.

Note that while invoking pip freeze in bash version may seem inefficient, only this method proves to be sufficiently robust to package naming peculiarities and inconsistencies (e.g. underscores vs dashes, small vs large caps, and abbreviations such as sklearn vs scikit-learn).

Caution: in complex environments both variants can return surprise version numbers, inconsistent with what you can actually get during import.

One such problem arises when there are other versions of the package hidden in a user site-packages subfolder. As an illustration of the perils of using version() here’s a situation I encountered:

$ pip freeze | grep lightgbm
lightgbm==2.3.1

and

$ python -c "import lightgbm; print(lightgbm.__version__)"
2.3.1

vs.

$ python -c "from importlib_metadata import version; print(version(\"lightgbm\"))"
2.2.3

until you delete the subfolder with the old version (here 2.2.3) from the user folder (only one would normally be preserved by `pip` - the one installed as last with the `--user` switch):

$ ls /home/jovyan/.local/lib/python3.7/site-packages/lightgbm*
/home/jovyan/.local/lib/python3.7/site-packages/lightgbm-2.2.3.dist-info
/home/jovyan/.local/lib/python3.7/site-packages/lightgbm-2.3.1.dist-info

Another problem is having some conda-installed packages in the same environment. If they share dependencies with your pip-installed packages, and versions of these dependencies differ, you may get downgrades of your pip-installed dependencies.

To illustrate, the latest version of numpy available in PyPI on 04-01-2020 was 1.18.0, while at the same time Anaconda’s conda-forge channel had only 1.17.3 version on numpy as their latest. So when you installed a basemap package with conda (as second), your previously pip-installed numpy would get downgraded by conda to 1.17.3, and version 1.18.0 would become unavailable to the import function. In this case version() would be right, and pip freeze/conda list wrong:

$ python -c "from importlib_metadata import version; print(version(\"numpy\"))"
1.17.3

$ python -c "import numpy; print(numpy.__version__)"
1.17.3

$ pip freeze | grep numpy
numpy==1.18.0

$ conda list | grep numpy
numpy                     1.18.0                   pypi_0    pypi

回答 10

pip show在python 3.7中有效:

pip show selenium
Name: selenium
Version: 4.0.0a3
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: c:\python3.7\lib\site-packages\selenium-4.0.0a3-py3.7.egg
Requires: urllib3
Required-by:

pip show works in python 3.7:

pip show selenium
Name: selenium
Version: 4.0.0a3
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: c:\python3.7\lib\site-packages\selenium-4.0.0a3-py3.7.egg
Requires: urllib3
Required-by:

回答 11

为此,请使用Python代码:

使用 importlib.metadata.version

Python≥3.8

import importlib.metadata
importlib.metadata.version('beautifulsoup4')
'4.9.1'

Python≤3.7

(使用importlib_metadata.version

!pip install importlib-metadata

import importlib_metadata
importlib_metadata.version('beautifulsoup4')
'4.9.1'

使用 pkg_resources.Distribution

import pkg_resources
pkg_resources.get_distribution('beautifulsoup4').version
'4.9.1'
pkg_resources.get_distribution('beautifulsoup4').parsed_version
<Version('4.9.1')>

归功于sinorocmirekphd的评论。

To do this using Python code:

Using importlib.metadata.version

Python ≥3.8

import importlib.metadata
importlib.metadata.version('beautifulsoup4')
'4.9.1'

Python ≤3.7

(using importlib_metadata.version)

!pip install importlib-metadata

import importlib_metadata
importlib_metadata.version('beautifulsoup4')
'4.9.1'

Using pkg_resources.Distribution

import pkg_resources
pkg_resources.get_distribution('beautifulsoup4').version
'4.9.1'
pkg_resources.get_distribution('beautifulsoup4').parsed_version
<Version('4.9.1')>

Credited to comments by sinoroc and mirekphd.


回答 12

对于Windows,您可以

  1. 打开cmd并键入python,然后按Enter。

  2. 键入导入,然后按Enter。

  3. 输入._version__并按Enter。

如您在此处的屏幕快照中所见,我正在使用此方法检查串行模块的版本。


For Windows you can

  1. open cmd and type python, press enter.

  2. type the import and press enter.

  3. type ._version__ and press enter.

As you can see in screen shot here I am using this method for checking the version of serial module.



回答 13

有问题的是,没有提到哪个OS用户正在使用(Windows / Linux / Mac)

因为有几个答案可以在Mac和Linux上完美运行。

如果用户试图在Windows上查找python软件包的版本,可以使用以下命令。

在PowerShell中,使用以下命令:

pip list | findstr <PackageName>

例:- pip list | findstr requests

输出: requests 2.18.4

In question, it is not mentioned which OS user is using (Windows/Linux/Mac)

As there are couple of answers which will work flawlessly on Mac and Linux.

Below command can be used in case the user is trying to find the version of a python package on windows.

In PowerShell use below command :

pip list | findstr <PackageName>

Example:- pip list | findstr requests

Output : requests 2.18.4