Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

python - 如何检查python模块的版本?(How to check version of python modules?)

I just installed the python modules: construct and statlib with setuptools like this:

(我刚刚安装了python模块: constructstatlib以及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

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 --version我可以从命令行运行?)

My python version is 2.7.3 .

(我的python版本是2.7.3 。)

  ask by tarabyte translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest using pip in place of easy_install .

(我建议使用pip代替easy_install 。)

With pip, you can list all installed packages and their versions with

(使用pip,您可以列出所有已安装的软件包及其版本)

pip freeze

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系统中,您可以将其传递给grep (或Windows上的findstr )以查找您感兴趣的特定包的行:)

Linux:
$ pip freeze | grep lxml
lxml==2.3

Windows:
c:> pip freeze | findstr lxml
lxml==2.3

For an individual module, you can try the __version__ attribute , however there are modules without it:

(对于单个模块,您可以尝试__version__属性 ,但是有没有它的模块:)

$ 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__'

Lastly, as the commands in your question are prefixed with sudo , it appears you're installing to the global python environment.

(最后,由于您的问题中的命令以sudo为前缀,您似乎正在安装到全局python环境中。)

Strongly advise to take look into python virtual environment managers, for example virtualenvwrapper

(强烈建议您查看python 虚拟环境管理器,例如virtualenvwrapper)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...