我刚刚安装了python模块:construct
和statlib
使用setuptools
如下所示:
# Install setuptools to be able to download the following
sudo apt-get install python-setuptools
# Install statlib for lightweight statistical tools
sudo easy_install statlib
# Install construct for packing/unpacking binary data
sudo easy_install construct
我希望能够(以编程方式)检查他们的版本。是否有等同于python --version
我可以从命令行运行吗?
我的python版本是2.7.3
。
发布于 2013-11-25 04:30:33
我建议使用代替easy\_install](https://stackoverflow.com/a/3220572/1265154)的[pip。使用pip,您可以使用以下命令列出所有已安装的软件包及其版本
pip freeze
在大多数linux系统中,您可以通过管道将其发送到grep
(或findstr
在Windows上)查找您感兴趣的特定包的行:
Linux:
$ pip freeze | grep lxml
lxml==2.3
Windows:
c:\> pip freeze | findstr lxml
lxml==2.3
对于单个模块,您可以尝试使用__version__
attribute但是,有一些模块没有它:
$ python -c "import requests; print(requests.__version__)"
2.14.2
$ python -c "import lxml; print(lxml.__version__)"
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute '__version__'
最后,由于问题中的命令前缀为sudo
,看起来您正在安装到全局python环境中。强烈建议研究python虚拟environment例如,经理virtualenvwrapper
发布于 2013-11-25 04:28:04
你可以试试
>>> import statlib
>>> print statlib.__version__
>>> import construct
>>> print contruct.__version__
更新:这是PEP 396推荐的方法。但PEP从未被接受,并已被推迟。事实上,在Python核心开发人员中,似乎有越来越多的人建议注释包括一个__version__
属性,例如在https://gitlab.com/python-devs/importlib\_metadata/-/merge\_requests/125..。
发布于 2015-10-06 16:46:29
Python >= 3.8:
如果你用的是python>=3.8
为此,您可以使用内置库中的模块。检查包的版本(在此示例中construct
)运行:
>>> from importlib.metadata import version
>>> version('construct')
'4.3.1'
Python < 3.8:
使用pkg_resources
随一起分发的模块setuptools
库。请注意,您传递到的字符串get_distribution
方法应该与PyPI条目相对应。
>>> import pkg_resources
>>> pkg_resources.get_distribution('construct').version
'2.5.2'
附注:
get_distribution
方法应为包在PyPI中注册的名称,而不是您尝试导入的模块名称。不幸的是,它们并不总是相同的(例如,你确实是这样做的pip install memcached
,但是import memcache
)。python -c \
"import pkg_resources; print(pkg_resources.get_distribution('construct').version)"
https://stackoverflow.com/questions/20180543
复制相似问题