我使用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 shell(在您感兴趣的Python版本中),导入库,并获取其__version__属性。
>>> import statlib
>>> statlib.__version__
>>> import construct
>>> contruct.__version__
注1:我们必须考虑Python版本。如果已经安装了不同版本的Python,则必须在感兴趣的Python版本中打开终端。例如,使用Python 3.8打开终端可以(肯定会)提供与使用Python 3.5或Python 2.7打开不同版本的库。
注意2:我们避免使用print函数,因为它的行为依赖于Python 2或Python 3。我们不需要它,终端将显示表达式的值。
其他回答
您可以简单地使用子流程。getoutput (python3——版本):
import subprocess as sp
print(sp.getoutput(python3 --version))
# Or however it suits your needs!
py3_version = sp.getoutput(python3 --version)
def check_version(name, version):...
check_version('python3', py3_version)
有关更多信息和不依赖__version__属性的方法:
指定os的输出。系统的一个变量,并防止它显示在屏幕上
你也可以使用subprocess.check_output(),当子进程返回的不是退出码0时,它会引发一个错误:
subprocess—子进程管理
一个列出所有包的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
你可以试试
>>> import statlib
>>> print statlib.__version__
>>> import construct
>>> print contruct.__version__
这是PEP 396推荐的方法。但是PEP从未被接受,并且一直被推迟。事实上,Python核心开发人员似乎越来越支持不包含__version__属性,例如在Remove importlib_metadata.version..
假设我们使用的是Jupyter Notebook(如果使用的是Terminal,去掉感叹号):
如果软件包(例如xgboost)与pip一起安装: 皮普秀xgboost !pip freeze | grep xgboost !pip list | grep xgboost 如果这个包(例如caffe)是用Conda安装的: conda list caffe
前面的答案没有解决我的问题,但这段代码做到了:
import sys
for name, module in sorted(sys.modules.items()):
if hasattr(module, '__version__'):
print name, module.__version__