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

sudo apt-get install python-setuptools

sudo easy_install statlib
sudo easy_install construct

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


当前回答

如果您的生产系统被强化到无法理解的地步,所以它既没有pip也没有conda,这里有一个Bash替代pip冻结:

ls /usr/local/lib/python3.8/dist-packages | grep info | awk -F "-" '{print $1"=="$2}' | sed 's/.dist//g'

(请确保将dist-packages文件夹更新到当前的python版本,并忽略不一致的名称,例如,下划线vs.破折号)。

样本打印:

Flask==1.1.2
Flask_Caching==1.10.1
gunicorn==20.1.0
[..]

其他回答

你可以试试

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

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

这是PEP 396推荐的方法。但是PEP从未被接受,并且一直被推迟。事实上,Python核心开发人员似乎越来越支持不包含__version__属性,例如在Remove importlib_metadata.version..

如果前面答案中的方法不起作用,那么值得在Python中尝试以下方法:

import modulename

modulename.version
modulename.version_info

参见获取Python Tornado版本

注意,除了Tornado之外,.版本也适用于其他几个版本。

这个答案是为Windows用户准备的。正如在所有其他答案中建议的那样,你可以这样使用语句:

import [type the module name]
print(module.__version__) # module + '.' + double underscore + version + double underscore

但是,有一些模块即使在使用上面的方法后也不打印它们的版本。所以,你可以简单地做:

Open the command prompt. Navigate to the file address/directory by using cd (file address) where you've kept your Python and all supporting modules installed. If you have only one Python interpreter on your system, the PyPI packages are normally visible in the directory/folder: Python → Lib → site-packages. use the command "pip install [module name]" and hit Enter. This will show you a message as "Requirement already satisfied: file address\folder name (with version)". See the screenshot below for example: I had to know the version of a pre-installed module named "Selenium-Screenshot". It correctly showed as 1.5.0:

在Python 3中用括号括起来的print:

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

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

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