我使用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
如何从命令行检查它们的版本?
当前回答
下面是一个小Bash程序,用于获取Python环境中任何包的版本。只需复制到你的/usr/bin,并为其提供可执行权限:
#!/bin/bash
packageName=$1
python -c "import ${packageName} as package; print(package.__version__)"
然后你可以在终端中运行它,假设你将脚本命名为py-check-version:
py-check-version whatever_package
其他回答
一个列出所有包的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查找版本!
# 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中用于过滤出版本并显示它。
对于未定义字段__version__的情况:
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata # python<=3.7
metadata.version("package")
或者,就像之前提到的那样:
import pkg_resources
pkg_resources.get_distribution('package').version
首先将可执行文件python和pip添加到环境变量中。这样您就可以从命令提示符执行您的命令。然后简单地给出Python命令。
然后导入包:
import scrapy
然后打印版本名
print(scrapy.__version__)
这肯定有用。