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

sudo apt-get install python-setuptools

sudo easy_install statlib
sudo easy_install construct

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


当前回答

模块。__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', ...]

其他回答

你可以试试这个:

pip list

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

输出

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

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

这个答案是为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:

你可以先安装这样的包,然后检查它的版本:

pip install package
import package
print(package.__version__)

它会给你一个打包的版本。

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

import modulename

modulename.version
modulename.version_info

参见获取Python Tornado版本

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