如何检查python模块的版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20180543/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:46:43  来源:igfitidea点击:

How to check version of python modules?

python

提问by tarabyte

I just installed the python modules: constructand statlibwith setuptoolslike this:

我刚安装的Python模块:constructstatlibsetuptools这样的:

# 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 --versionI can run from the command line?

我希望能够(以编程方式)检查他们的版本。是否有相当于python --version我可以从命令行运行的?

My python version is 2.7.3.

我的 python 版本是2.7.3.

采纳答案by alko

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(or findstron Windows) to find the row for the particular package you're interested in:

在大多数 linux 系统中,您可以通过管道将其发送到grep(或findstr在 Windows 上)以查找您感兴趣的特定包的行:

Linux:
$ pip freeze | grep lxml
lxml==2.3

Windows:
c:\> pip freeze | findstr 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 environmentmanagers, for example virtualenvwrapper

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

回答by damienfrancois

You can try

你可以试试

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

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

回答by 0x3bfc

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

我认为这会有所帮助,但首先安装show软件包才能运行,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

回答by user224767

In python3 with brackets around print

在 python3 中,打印周围带有括号

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

回答by tashuhka

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__ 

回答by D Adams

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 Tornado 版本?

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

请注意,.version除了龙卷风之外,还有其他一些对我有用。

回答by waterproof

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

module.__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_distributions()从 Python 中获取版本:

update:the solution here works in pip 8 and 9, but in pip 10 the function has been moved from pip.get_installed_distributionsto pip._internal.utils.misc.get_installed_distributionsto 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_distributionspip._internal.utils.misc.get_installed_distributions明确表示,这不是外用。如果您使用的是 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', ...]

回答by Jakub Kukul

Use pkg_resourcesmodule distributed with setuptoolslibrary. Note that the string that you pass to get_distributionmethod should correspond to the PyPI entry.

使用pkg_resourcessetuptools库分发的模块。请注意,您传递给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)"

Note that the string that you pass to the get_distributionmethod should be the package name as registered in PyPI, not the module name that you are trying to import.

请注意,您传递给该get_distribution方法的字符串应该是在 PyPI 中注册的包名称,而不是您尝试导入的模块名称。

Unfortunately these aren't always the same (e.g. you do pip install memcached, but import memcache).

不幸的是,这些并不总是相同的(例如,您这样做pip install memcached,但是import memcache)。

回答by Yuchao Jiang

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

有些模块没有__version__属性,所以最简单的方法是在终端中检查:pip list

回答by Tobias Bleisch

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?

这段代码是从这个页面上的解决方案和如何列出导入的模块?