如何检查python模块的版本? [英] How to check version of python modules?
问题描述
我刚刚安装了 python 模块:construct
和 statlib
和 setuptools
像这样:
I just installed the python modules: construct
and statlib
with setuptools
like this:
# 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
我可以从命令行运行?
I want to be able to (programmatically) check their versions. Is there an equivalent to python --version
I can run from the command line?
我的python版本是2.7.3
.
My python version is 2.7.3
.
推荐答案
我建议使用 pip 代替 easy_install.使用 pip,您可以使用
I suggest using pip in place of easy_install. With pip, you can list all installed packages and their versions with
pip freeze
在大多数 linux 系统中,您可以将其传送到 grep
(或 Windows 上的 findstr
)以查找您感兴趣的特定包的行:>
In most linux systems, you can pipe this to grep
(or findstr
on Windows) to find the row for the particular package you're interested in:
Linux:
$ pip freeze | grep lxml
lxml==2.3
Windows:
c:\> pip freeze | findstr lxml
lxml==2.3
对于单个模块,您可以尝试 __version__
属性,但是也有没有它的模块:
For an individual module, you can try the __version__
attribute, however there are modules without it:
$ python -c "import requests; print(requests.__version__)"
2.14.2
$ python -c "import lxml; print(lxml.__version__)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
最后,由于您问题中的命令以 sudo
为前缀,因此您似乎正在安装到全局 python 环境.强烈建议查看 python 虚拟环境 管理器,例如 virtualenvwrapper
Lastly, as the commands in your question are prefixed with sudo
, it appears you're installing to the global python environment. Strongly advise to take look into python virtual environment managers, for example virtualenvwrapper
这篇关于如何检查python模块的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!