如何检查python模块的版本?

时间:2021-09-27 07:02:44

I just installed the python modules: construct and statlib with setuptools like this:

我刚刚用setuptools安装了python模块:construct和statlib,如下所示:

# Install setuptools to be able to download the following
sudo apt-get install python-setuptools

# Install statlib for lightweight statistical tools
sudo easy_install statlib

# Install construct for packing/unpacking binary data
sudo easy_install construct

I want to be able to (programmatically) check their versions. Is there an equivalent to python --version I can run from the command line?

我希望能够(通过编程)检查它们的版本。是否有一个与python相同的版本——我可以从命令行运行的版本?

My python version is 2.7.3.

我的python版本是2.7.3。

13 个解决方案

#1


371  

I suggest using pip in place of easy_install. With pip, you can list all installed packages and their versions with

我建议使用pip代替easy_install。使用pip,您可以列出所有已安装的包及其版本

pip freeze

In most linux systems, you can pipe this to grep to find the row for the particular package you're interested in:

在大多数linux系统中,您都可以将其传输到grep,以找到您感兴趣的特定包的行:

$ pip freeze | grep lxml
lxml==2.3

For an individual module, you can try the __version__ attribute, however there are modules without it:

对于单独的模块,您可以尝试__version__属性,但是有没有它的模块:

$ python -c "import requests; print(requests.__version__)"
2.14.2
$ python -c "import lxml; print(lxml.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'

Lastly, as the commands in your question are prefixed with sudo, it appears you're installing to the global python environment. Strongly advise to take look into python virtual environment managers, for example virtualenvwrapper

最后,由于问题中的命令以sudo作为前缀,所以看起来您正在安装到全局python环境中。强烈建议查看python虚拟环境管理器,例如virtualenvwrapper。

#2


245  

You can try

你可以试着

>>> import statlib
>>> print statlib.__version__

>>> import construct
>>> print contruct.__version__

#3


92  

Use pkg_resources module distributed with setuptools library. Note that the string that you pass to get_distribution method should correspond to the PyPI entry.

使用与setuptools库一起分发的pkg_resources模块。注意,传递给get_distribution方法的字符串应该与PyPI条目对应。

>>> import pkg_resources
>>> pkg_resources.get_distribution("construct").version
'2.5.2'

and if you want to run it from the command line you can do:

如果你想从命令行运行它,你可以:

python -c "import pkg_resources; print pkg_resources.get_distribution('construct').version"

(Disclaimer: This is pretty much a repost of this answer, but to me it is more relevant than any other answer to this question.)

(免责声明:这几乎是对这个答案的重新发布,但对我来说,它比这个问题的任何其他答案都更有意义。)

#4


70  

I think this can help but first install show package in order to run pip show then use show to find the version!

我认为这可以帮助但是首先安装show package来运行pip show然后使用show来找到版本!

sudo pip install show
# in order to get package version execute the below command
sudo pip show YOUR_PACKAGE_NAME | grep Version

#5


25  

In python3 with brackets around print

在python3里,用括号括住打印

>>> import celery
>>> print(celery.__version__)
3.1.14

#6


14  

module.__version__ is a good first thing to try, but it doesn't always work.

模块。__version__是一个很好的尝试,但它并不总是有效。

If you don't want to shell out, and you're using pip 8 or 9, you can still use pip.get_installed_distributions() to get versions from within Python:

如果您不想要支付,并且您正在使用pip 8或9,您仍然可以使用pip.get_installed_distribution()从Python中获取版本:

update: the solution here works in pip 8 and 9, but in pip 10 the function has been moved from pip.get_installed_distributions to pip._internal.utils.misc.get_installed_distributions to explicitly indicate that it's not for external use. It's not a good idea to rely on it if you're using pip 10+.

更新:这里的解决方案在pip 8和9中工作,但是在pip 10中,函数已经从pip移动。get_installed_distributions pip._internal.utils.misc。get_installed_distribution显式地指示它不用于外部使用。如果你使用的是pip 10+,那么依赖它不是一个好主意。

import pip

pip.get_installed_distributions()  # -> [distribute 0.6.16 (...), ...]

[
    pkg.key + ': ' + pkg.version
    for pkg in pip.get_installed_distributions()
    if pkg.key in ['setuptools', 'statlib', 'construct']
] # -> nicely filtered list of ['setuptools: 3.3', ...]

#7


12  

The previous answers did not solve my problem, but this code did:

前面的答案并不能解决我的问题,但是这个代码可以:

import sys 
for name, module in sorted(sys.modules.items()): 
  if hasattr(module, '__version__'): 
    print name, module.__version__ 

#8


12  

The Better way to do that is:

更好的方法是:


For the details of specific Package

有关特定包装的详情

pip show <package_name>

皮普显示< package_name >

It details out the Package_name, Version, Author, Location etc.

它详细描述了Package_name、版本、作者、位置等。


$ pip show numpy
Name: numpy
Version: 1.13.3
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: http://www.numpy.org
Author: NumPy Developers
Author-email: numpy-discussion@python.org
License: BSD
Location: c:\users\prowinjvm\appdata\local\programs\python\python36\lib\site-packages
Requires:

For more Details: >>> pip help

更多细节:>>> pip帮助


#9


5  

If the above methods do not work, it is worth trying the following in python:

如果以上方法不起作用,那么在python中尝试以下方法是值得的:

import modulename

modulename.version
modulename.version_info

See Get Python Tornado Version?

看到Python的龙卷风版本了吗?

Note, the .version worked for me on a few others besides tornado as well.

注意,除了tornado之外,我还可以使用其他几个版本。

#10


4  

first add python, pip to your environment variables. so that you can execute your commands from command prompt. then simply give python command. then import the package

首先添加python, pip到环境变量中。这样您就可以从命令提示符执行命令。然后简单地给python命令。然后导入包

-->import scrapy

- - >导入scrapy

then print the version name

然后打印版本名

-->print(scrapy.__version__)

- - >打印(scrapy.__version__)

This will definitely work

这肯定会工作

#11


3  

Some modules don't have __version__ attribute, so the easiest way is check in the terminal: pip list

有些模块没有__version__属性,所以最简单的方法是签入终端:pip列表

#12


1  

To get a list of non-standard (pip) modules imported in the current module:

获取当前模块中导入的非标准(pip)模块列表:

[{pkg.key : pkg.version} for pkg in pip.get_installed_distributions() 
   if pkg.key in set(sys.modules) & set(globals())]

Result:

结果:

>>> import sys, pip, nltk, bs4
>>> [{pkg.key : pkg.version} for pkg in pip.get_installed_distributions() if pkg.key in set(sys.modules) & set(globals())]
[{'pip': '9.0.1'}, {'nltk': '3.2.1'}, {'bs4': '0.0.1'}]

Note:

注意:

This code was put together from solutions both on this page and from How to list imported modules?

这段代码是由这个页面上的解决方案和如何列出导入模块组成的。

#13


0  

I had the same problem, I tried to uninstall both modules: serialand pyserial. Then I reinstalled pyserial ONLY and it worked perfectly.

我遇到了同样的问题,我试图卸载两个模块:serialand pyserial。然后我只重新安装了pyserial,它运行得很好。

#1


371  

I suggest using pip in place of easy_install. With pip, you can list all installed packages and their versions with

我建议使用pip代替easy_install。使用pip,您可以列出所有已安装的包及其版本

pip freeze

In most linux systems, you can pipe this to grep to find the row for the particular package you're interested in:

在大多数linux系统中,您都可以将其传输到grep,以找到您感兴趣的特定包的行:

$ pip freeze | grep lxml
lxml==2.3

For an individual module, you can try the __version__ attribute, however there are modules without it:

对于单独的模块,您可以尝试__version__属性,但是有没有它的模块:

$ python -c "import requests; print(requests.__version__)"
2.14.2
$ python -c "import lxml; print(lxml.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'

Lastly, as the commands in your question are prefixed with sudo, it appears you're installing to the global python environment. Strongly advise to take look into python virtual environment managers, for example virtualenvwrapper

最后,由于问题中的命令以sudo作为前缀,所以看起来您正在安装到全局python环境中。强烈建议查看python虚拟环境管理器,例如virtualenvwrapper。

#2


245  

You can try

你可以试着

>>> import statlib
>>> print statlib.__version__

>>> import construct
>>> print contruct.__version__

#3


92  

Use pkg_resources module distributed with setuptools library. Note that the string that you pass to get_distribution method should correspond to the PyPI entry.

使用与setuptools库一起分发的pkg_resources模块。注意,传递给get_distribution方法的字符串应该与PyPI条目对应。

>>> import pkg_resources
>>> pkg_resources.get_distribution("construct").version
'2.5.2'

and if you want to run it from the command line you can do:

如果你想从命令行运行它,你可以:

python -c "import pkg_resources; print pkg_resources.get_distribution('construct').version"

(Disclaimer: This is pretty much a repost of this answer, but to me it is more relevant than any other answer to this question.)

(免责声明:这几乎是对这个答案的重新发布,但对我来说,它比这个问题的任何其他答案都更有意义。)

#4


70  

I think this can help but first install show package in order to run pip show then use show to find the version!

我认为这可以帮助但是首先安装show package来运行pip show然后使用show来找到版本!

sudo pip install show
# in order to get package version execute the below command
sudo pip show YOUR_PACKAGE_NAME | grep Version

#5


25  

In python3 with brackets around print

在python3里,用括号括住打印

>>> import celery
>>> print(celery.__version__)
3.1.14

#6


14  

module.__version__ is a good first thing to try, but it doesn't always work.

模块。__version__是一个很好的尝试,但它并不总是有效。

If you don't want to shell out, and you're using pip 8 or 9, you can still use pip.get_installed_distributions() to get versions from within Python:

如果您不想要支付,并且您正在使用pip 8或9,您仍然可以使用pip.get_installed_distribution()从Python中获取版本:

update: the solution here works in pip 8 and 9, but in pip 10 the function has been moved from pip.get_installed_distributions to pip._internal.utils.misc.get_installed_distributions to explicitly indicate that it's not for external use. It's not a good idea to rely on it if you're using pip 10+.

更新:这里的解决方案在pip 8和9中工作,但是在pip 10中,函数已经从pip移动。get_installed_distributions pip._internal.utils.misc。get_installed_distribution显式地指示它不用于外部使用。如果你使用的是pip 10+,那么依赖它不是一个好主意。

import pip

pip.get_installed_distributions()  # -> [distribute 0.6.16 (...), ...]

[
    pkg.key + ': ' + pkg.version
    for pkg in pip.get_installed_distributions()
    if pkg.key in ['setuptools', 'statlib', 'construct']
] # -> nicely filtered list of ['setuptools: 3.3', ...]

#7


12  

The previous answers did not solve my problem, but this code did:

前面的答案并不能解决我的问题,但是这个代码可以:

import sys 
for name, module in sorted(sys.modules.items()): 
  if hasattr(module, '__version__'): 
    print name, module.__version__ 

#8


12  

The Better way to do that is:

更好的方法是:


For the details of specific Package

有关特定包装的详情

pip show <package_name>

皮普显示< package_name >

It details out the Package_name, Version, Author, Location etc.

它详细描述了Package_name、版本、作者、位置等。


$ pip show numpy
Name: numpy
Version: 1.13.3
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: http://www.numpy.org
Author: NumPy Developers
Author-email: numpy-discussion@python.org
License: BSD
Location: c:\users\prowinjvm\appdata\local\programs\python\python36\lib\site-packages
Requires:

For more Details: >>> pip help

更多细节:>>> pip帮助


#9


5  

If the above methods do not work, it is worth trying the following in python:

如果以上方法不起作用,那么在python中尝试以下方法是值得的:

import modulename

modulename.version
modulename.version_info

See Get Python Tornado Version?

看到Python的龙卷风版本了吗?

Note, the .version worked for me on a few others besides tornado as well.

注意,除了tornado之外,我还可以使用其他几个版本。

#10


4  

first add python, pip to your environment variables. so that you can execute your commands from command prompt. then simply give python command. then import the package

首先添加python, pip到环境变量中。这样您就可以从命令提示符执行命令。然后简单地给python命令。然后导入包

-->import scrapy

- - >导入scrapy

then print the version name

然后打印版本名

-->print(scrapy.__version__)

- - >打印(scrapy.__version__)

This will definitely work

这肯定会工作

#11


3  

Some modules don't have __version__ attribute, so the easiest way is check in the terminal: pip list

有些模块没有__version__属性,所以最简单的方法是签入终端:pip列表

#12


1  

To get a list of non-standard (pip) modules imported in the current module:

获取当前模块中导入的非标准(pip)模块列表:

[{pkg.key : pkg.version} for pkg in pip.get_installed_distributions() 
   if pkg.key in set(sys.modules) & set(globals())]

Result:

结果:

>>> import sys, pip, nltk, bs4
>>> [{pkg.key : pkg.version} for pkg in pip.get_installed_distributions() if pkg.key in set(sys.modules) & set(globals())]
[{'pip': '9.0.1'}, {'nltk': '3.2.1'}, {'bs4': '0.0.1'}]

Note:

注意:

This code was put together from solutions both on this page and from How to list imported modules?

这段代码是由这个页面上的解决方案和如何列出导入模块组成的。

#13


0  

I had the same problem, I tried to uninstall both modules: serialand pyserial. Then I reinstalled pyserial ONLY and it worked perfectly.

我遇到了同样的问题,我试图卸载两个模块:serialand pyserial。然后我只重新安装了pyserial,它运行得很好。