我使用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中用于过滤出版本并显示它。

其他回答

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

然后导入包:

import scrapy

然后打印版本名

print(scrapy.__version__)

这肯定有用。

像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

希望能帮助到大家, 问候

你可以试试

>>> import statlib
>>> print statlib.__version__

>>> import construct
>>> print contruct.__version__

这是PEP 396推荐的方法。但是PEP从未被接受,并且一直被推迟。事实上,Python核心开发人员似乎越来越支持不包含__version__属性,例如在Remove importlib_metadata.version..

你可以先安装这样的包,然后检查它的版本:

pip install package
import package
print(package.__version__)

它会给你一个打包的版本。

根据Jakub Kukul的回答,我找到了一个更可靠的方法来解决这个问题。

这种方法的主要问题是要求包按“常规”安装(不包括使用pip install——user),或者在Python初始化时位于系统PATH中。

要解决这个问题,可以使用pkg_resources. find_distributors (path_to_search)。如果path_to_search在系统PATH中,它基本上会搜索可导入的分布。

我们可以像这样遍历这个生成器:

avail_modules = {}
distros = pkg_resources.find_distributions(path_to_search)
for d in distros:
    avail_modules[d.key] = d.version

这将返回一个以模块为键,其版本为值的字典。这种方法可以扩展到比版本号更多的版本。

感谢Jakub Kukul指出了正确的方向。