我使用setuptools安装了Python模块construct和statlib:

sudo apt-get install python-setuptools

sudo easy_install statlib
sudo easy_install construct

如何从命令行检查它们的版本?


当前回答

我自己在一个受到严格限制的服务器环境中工作,不幸的是,这里的解决方案都不适合我。可能没有适合所有情况的全局解决方案,但我找到了一个快速的解决方案,即在脚本中读取pip freeze的终端输出,并将模块标签和版本存储在字典中。

import os
os.system('pip freeze > tmpoutput')
with open('tmpoutput', 'r') as f:
    modules_version = f.read()
  
module_dict = {item.split("==")[0]:item.split("==")[-1] for item in modules_versions.split("\n")}

通过传递模块标签键来检索模块的版本,例如:

>>  module_dict["seaborn"]
'0.9.0'

其他回答

使用pip show查找版本!

# In order to get the package version, execute the below command
pip show YOUR_PACKAGE_NAME | grep Version

您可以使用pip show YOUR_PACKAGE_NAME -它会提供包的所有细节。这也适用于Windows。

grep Version在Linux中用于过滤出版本并显示它。

你可以试试这个:

pip list

这将输出所有包及其版本。

输出

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

如果你不想退出,并且你正在使用pip 8或9,你仍然可以使用pip.get_installed_distribution()从Python内部获取版本:

这里的解决方案适用于pip 8和9,但在pip 10中,该函数已从pip中移动。get_installed_distribution到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', ...]

前面的答案没有解决我的问题,但这段代码做到了:

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

我自己在一个受到严格限制的服务器环境中工作,不幸的是,这里的解决方案都不适合我。可能没有适合所有情况的全局解决方案,但我找到了一个快速的解决方案,即在脚本中读取pip freeze的终端输出,并将模块标签和版本存储在字典中。

import os
os.system('pip freeze > tmpoutput')
with open('tmpoutput', 'r') as f:
    modules_version = f.read()
  
module_dict = {item.split("==")[0]:item.split("==")[-1] for item in modules_versions.split("\n")}

通过传递模块标签键来检索模块的版本,例如:

>>  module_dict["seaborn"]
'0.9.0'