我使用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
如何从命令行检查它们的版本?
当前回答
使用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中用于过滤出版本并显示它。
其他回答
这个答案是为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而不是easy_install。
使用pip,通过以下方式列出所有已安装的软件包及其版本:
pip freeze
在大多数Linux系统上,您可以将其输送到grep(或Windows上的findstr),以查找您感兴趣的特定包的行。
Linux:
pip freeze | grep lxml
lxml = = 2.3
窗口:
pip freeze | findstr lxml
lxml = = 2.3
对于单个模块,您可以尝试__version__属性。然而,有一些模块没有它:
python -c "import requests; print(requests.__version__)"
2.14.2
python -c "import lxml; print(lxml.__version__)"
回溯(最近一次调用): 文件"<string>",第1行,在<模块> 'module'对象没有'version'属性
最后,由于您的问题中的命令带有sudo前缀,似乎您正在安装到全局python环境。我强烈建议研究一下Python虚拟环境管理器,例如virtualenvwrapper。
对于未定义字段__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
你可以试试
>>> import statlib
>>> print statlib.__version__
>>> import construct
>>> print contruct.__version__
这是PEP 396推荐的方法。但是PEP从未被接受,并且一直被推迟。事实上,Python核心开发人员似乎越来越支持不包含__version__属性,例如在Remove importlib_metadata.version..
前面的答案没有解决我的问题,但这段代码做到了:
import sys
for name, module in sorted(sys.modules.items()):
if hasattr(module, '__version__'):
print name, module.__version__