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

sudo apt-get install python-setuptools

sudo easy_install statlib
sudo easy_install construct

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


当前回答

Python >= 3.8:

如果您使用的是Python >= 3.8,则可以使用内置库中的模块。要检查包的版本(在这个例子中),运行:

>>> from importlib.metadata import version
>>> version('construct')
'4.3.1'

Python < 3.8:

使用随setuptools库分发的pkg_resources模块。注意,传递给get_distribution方法的字符串应该对应于PyPI条目。

>>> import pkg_resources
>>> pkg_resources.get_distribution('construct').version
'2.5.2'

边注:

请注意,传递给get_distribution方法的字符串应该是在PyPI中注册的包名,而不是您试图导入的模块名。不幸的是,它们并不总是相同的(例如,你安装了memcached,但导入了memcache)。 如果你想从命令行应用这个解决方案,你可以这样做:

python -c \
  "import pkg_resources; print(pkg_resources.get_distribution('construct').version)"

其他回答

首先将可执行文件python和pip添加到环境变量中。这样您就可以从命令提示符执行您的命令。然后简单地给出Python命令。

然后导入包:

import scrapy

然后打印版本名

print(scrapy.__version__)

这肯定有用。

对于未定义字段__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

这工作在Jupyter笔记本上的Windows,太!只要Jupyter是从兼容Bash的命令行(如Git Bash (Mingw-w64))启动的,许多答案中给出的解决方案都可以在Windows系统上的Jupyter Notebook中使用,只需稍加调整。

我运行的是通过Anaconda安装Python的Windows 10 Pro,当我通过Git Bash启动Jupyter时,下面的代码可以工作(但当我从Anaconda提示启动时不能工作)。

调整:在pip前面加一个感叹号(!),使它成为!pip。

>>>!pip show lxml | grep Version
Version: 4.1.0

>>>!pip freeze | grep lxml
lxml==4.1.0

>>>!pip list | grep lxml
lxml                               4.1.0

>>>!pip show lxml
Name: lxml
Version: 4.1.0
Summary: Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API.
Home-page: http://lxml.de/
Author: lxml dev team
Author-email: lxml-dev@lxml.de
License: BSD
Location: c:\users\karls\anaconda2\lib\site-packages
Requires:
Required-by: jupyter-contrib-nbextensions

像pycharm-terminal一样去终端

现在编写py或python 并按Enter键。

现在你在终端的python中,你可以尝试这样做:

# import <name_of_the_library>

import kivy

# So if the library has __version__ magic method, so this way will help you

kivy.__version__  # then hit Enter to see the version

# Output >> '2.1.0'

但如果上面的方法不奏效,你可以试试这种方法,知道信息包括库的版本

 pip show module <HERE PUT THE NAME OF THE LIBRARY>

例子:

pip show module pyperclip

Output:
       Name: pyperclip
       Version: 1.8.2
       Summary: A cross-platform clipboard module for Python. (Only handles plain text for now.)
       Home-page: https://github.com/asweigart/pyperclip
       Author: Al Sweigart
       Author-email: al@inventwithpython.com
       License: BSD
       Location: c:\c\kivymd\virt\lib\site-packages
       Requires:
       Required-by:

还有一种方法可以帮助你在项目中显示所有的库和它们的版本:

pip freeze
# I used the above command in a terminal inside my project this is the output
       certifi==2021.10.8
       charset-normalizer==2.0.12
       docutils==0.18.1
       idna==3.3
       Kivy==2.1.0
       kivy-deps.angle==0.3.2
       kivy-deps.glew==0.3.1
       kivy-deps.sdl2==0.4.5
       Kivy-Garden==0.1.5
       kivymd @ file:///C:/c/kivymd/KivyMD
       Pillow==9.1.0
       Pygments==2.12.0
       pyperclip==1.8.2
       pypiwin32==223
       pywin32==303
       requests==2.27.1
       urllib3==1.26.9

当然,您可以尝试使用下面的命令来显示所有库及其版本

pip list

希望能帮助到大家, 问候

我自己在一个受到严格限制的服务器环境中工作,不幸的是,这里的解决方案都不适合我。可能没有适合所有情况的全局解决方案,但我找到了一个快速的解决方案,即在脚本中读取pip freeze的终端输出,并将模块标签和版本存储在字典中。

import os
os.system('pip freeze > tmpoutput')
with open('tmpoutput', 'r') as f:
    modules_version = f.read()
  
module_dict = {item.split("==")[0]:item.split("==")[-1] for item in modules_versions.split("\n")}

通过传递模块标签键来检索模块的版本,例如:

>>  module_dict["seaborn"]
'0.9.0'