我使用setuptools安装了Python模块construct和statlib:
sudo apt-get install python-setuptools
sudo easy_install statlib
sudo easy_install construct
如何从命令行检查它们的版本?
我使用setuptools安装了Python模块construct和statlib:
sudo apt-get install python-setuptools
sudo easy_install statlib
sudo easy_install construct
如何从命令行检查它们的版本?
当前回答
(请参见如何以编程方式获取Python中已安装模块的版本?)
我发现使用各种可用的工具(包括Jakub Kukul回答中提到的最好的pkg_resources)是非常不可靠的,因为大多数工具都不能覆盖所有情况。例如
内置模块 模块没有安装,只是添加到python路径(例如,通过IDE) 同一模块可用的两个版本(python路径中的一个取代已安装的版本)
因为我们需要一种可靠的方法来获取任何包、模块或子模块的版本,所以我最终编写了getversion。使用起来很简单:
from getversion import get_module_version
import foo
version, details = get_module_version(foo)
详细信息请参见文档。
其他回答
根据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指出了正确的方向。
使用pip而不是easy_install。
使用pip,通过以下方式列出所有已安装的软件包及其版本:
pip freeze
在大多数Linux系统上,您可以将其输送到grep(或Windows上的findstr),以查找您感兴趣的特定包的行。
Linux:
pip freeze | grep lxml
lxml = = 2.3
窗口:
pip freeze | findstr lxml
lxml = = 2.3
对于单个模块,您可以尝试__version__属性。然而,有一些模块没有它:
python -c "import requests; print(requests.__version__)"
2.14.2
python -c "import lxml; print(lxml.__version__)"
回溯(最近一次调用): 文件"<string>",第1行,在<模块> 'module'对象没有'version'属性
最后,由于您的问题中的命令带有sudo前缀,似乎您正在安装到全局python环境。我强烈建议研究一下Python虚拟环境管理器,例如virtualenvwrapper。
像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 show <package_name>
它详细说明了package_name、版本、作者、位置等。
$ pip show numpy
Name: numpy
Version: 1.13.3
Summary: NumPy: array processing for numbers, strings, records, and objects.
Home-page: http://www.numpy.org
Author: NumPy Developers
Author-email: numpy-discussion@python.org
License: BSD
Location: c:\users\prowinjvm\appdata\local\programs\python\python36\lib\site-packages
Requires:
查看详细信息:>>> pip help
应该更新PIP以实现这一点。
pip install --upgrade pip
在Windows上,推荐的命令是:
python -m pip install --upgrade pip
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)"