我使用setuptools安装了Python模块construct和statlib:
sudo apt-get install python-setuptools
sudo easy_install statlib
sudo easy_install construct
如何从命令行检查它们的版本?
我使用setuptools安装了Python模块construct和statlib:
sudo apt-get install python-setuptools
sudo easy_install statlib
sudo easy_install construct
如何从命令行检查它们的版本?
当前回答
一个列出所有包的Python程序(你可以把它复制到file requirements.txt):
from pip._internal.utils.misc import get_installed_distributions
print_log = ''
for module in sorted(get_installed_distributions(), key=lambda x: x.key):
print_log += module.key + '~=' + module.version + '\n'
print(print_log)
输出如下所示:
asn1crypto~=0.24.0
attrs~=18.2.0
automat~=0.7.0
beautifulsoup4~=4.7.1
botocore~=1.12.98
其他回答
在Python 3中用括号括起来的print:
>>> import celery
>>> print(celery.__version__)
3.1.14
更好的方法是:
获取特定包的详细信息
pip show <package_name>
它详细说明了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:
查看详细信息:>>> pip help
应该更新PIP以实现这一点。
pip install --upgrade pip
在Windows上,推荐的命令是:
python -m pip install --upgrade pip
你可以试试这个:
pip list
这将输出所有包及其版本。
输出
有些模块没有__version__属性,所以最简单的方法是在terminal: pip列表中检查
我自己在一个受到严格限制的服务器环境中工作,不幸的是,这里的解决方案都不适合我。可能没有适合所有情况的全局解决方案,但我找到了一个快速的解决方案,即在脚本中读取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'