我使用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
如何从命令行检查它们的版本?
当前回答
这个答案是为Windows用户准备的。正如在所有其他答案中建议的那样,你可以这样使用语句:
import [type the module name]
print(module.__version__) # module + '.' + double underscore + version + double underscore
但是,有一些模块即使在使用上面的方法后也不打印它们的版本。所以,你可以简单地做:
Open the command prompt. Navigate to the file address/directory by using cd (file address) where you've kept your Python and all supporting modules installed. If you have only one Python interpreter on your system, the PyPI packages are normally visible in the directory/folder: Python → Lib → site-packages. use the command "pip install [module name]" and hit Enter. This will show you a message as "Requirement already satisfied: file address\folder name (with version)". See the screenshot below for example: I had to know the version of a pre-installed module named "Selenium-Screenshot". It correctly showed as 1.5.0:
其他回答
使用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 3中用括号括起来的print:
>>> import celery
>>> print(celery.__version__)
3.1.14
如果您的生产系统被强化到无法理解的地步,所以它既没有pip也没有conda,这里有一个Bash替代pip冻结:
ls /usr/local/lib/python3.8/dist-packages | grep info | awk -F "-" '{print $1"=="$2}' | sed 's/.dist//g'
(请确保将dist-packages文件夹更新到当前的python版本,并忽略不一致的名称,例如,下划线vs.破折号)。
样本打印:
Flask==1.1.2
Flask_Caching==1.10.1
gunicorn==20.1.0
[..]
这工作在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
您可以简单地使用子流程。getoutput (python3——版本):
import subprocess as sp
print(sp.getoutput(python3 --version))
# Or however it suits your needs!
py3_version = sp.getoutput(python3 --version)
def check_version(name, version):...
check_version('python3', py3_version)
有关更多信息和不依赖__version__属性的方法:
指定os的输出。系统的一个变量,并防止它显示在屏幕上
你也可以使用subprocess.check_output(),当子进程返回的不是退出码0时,它会引发一个错误:
subprocess—子进程管理