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

sudo apt-get install python-setuptools

sudo easy_install statlib
sudo easy_install construct

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


当前回答

一个列出所有包的Python程序(你可以把它复制到file requirements.txt):

from pip._internal.utils.misc import get_installed_distributions
print_log = ''
for module in sorted(get_installed_distributions(), key=lambda x: x.key):
    print_log +=  module.key + '~=' + module.version  + '\n'
print(print_log)

输出如下所示:

asn1crypto~=0.24.0
attrs~=18.2.0
automat~=0.7.0
beautifulsoup4~=4.7.1
botocore~=1.12.98

其他回答

获取当前模块中导入的非标准(pip)模块列表:

[{pkg.key : pkg.version} for pkg in pip.get_installed_distributions() 
   if pkg.key in set(sys.modules) & set(globals())]

结果:

>>> import sys, pip, nltk, bs4
>>> [{pkg.key : pkg.version} for pkg in pip.get_installed_distributions() if pkg.key in set(sys.modules) & set(globals())]
[{'pip': '9.0.1'}, {'nltk': '3.2.1'}, {'bs4': '0.0.1'}]

注意:

这段代码从解决方案放在一起在这个页面和如何列出导入的模块?

模块。__version__是一个很好的尝试,但它并不总是有效。

如果你不想退出,并且你正在使用pip 8或9,你仍然可以使用pip.get_installed_distribution()从Python内部获取版本:

这里的解决方案适用于pip 8和9,但在pip 10中,该函数已从pip中移动。get_installed_distribution到pip._internal.utils.misc。get_installed_distribution显式地指出它不供外部使用。如果您正在使用pip 10+,那么依赖它不是一个好主意。

import pip

pip.get_installed_distributions()  # -> [distribute 0.6.16 (...), ...]

[
    pkg.key + ': ' + pkg.version
    for pkg in pip.get_installed_distributions()
    if pkg.key in ['setuptools', 'statlib', 'construct']
] # -> nicely filtered list of ['setuptools: 3.3', ...]

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

pip install package
import package
print(package.__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

希望能帮助到大家, 问候

你可以试试这个:

pip list

这将输出所有包及其版本。

输出