如何通过代码获取python模块的版本号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3524168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a python module's version number through code?
提问by Joe Schmoe
I'm trying to get the version number of a specific few modules that I use. Something that I can store in a variable.
我正在尝试获取我使用的特定几个模块的版本号。我可以存储在变量中的东西。
采纳答案by Nick T
Generalized answer from Matt's, do a dir(YOURMODULE)and look for __version__, VERSION, or version. Most modules like __version__but I think numpyuses version.version
来自马特的一般答案,做 adir(YOURMODULE)并寻找__version__, VERSION, 或version。大多数模块都喜欢,__version__但我认为numpy使用version.version
回答by Matthew J Morrison
I think it depends on the module. For example, Django has a VERSION variable that you can get from django.VERSION, sqlalchemy has a __version__variable that you can get from sqlalchemy.__version__.
我认为这取决于模块。例如,Django 有一个 VERSION 变量可以从 中获取django.VERSION,sqlalchemy 有一个__version__变量可以从 中获取sqlalchemy.__version__。
回答by softvar
Use pkg_resources(part of setuptools). Anything installed from PyPIat least has a version number. No extra package/module is needed.
使用pkg_resources(setuptools 的一部分)。从PyPI安装的任何东西至少都有一个版本号。不需要额外的包/模块。
>>> import pkg_resources
>>> pkg_resources.get_distribution("simplegist").version
'0.3.2'
回答by phzx_munki
Some modules (e.g. azure) do not provide a __version__string.
某些模块(例如 azure)不提供__version__字符串。
If the package was installed with pip, the following should work.
如果该软件包是使用 pip 安装的,则以下内容应该可以工作。
# say we want to look for the version of the "azure" module
import pip
for m in pip.get_installed_distributions():
if m.project_name == 'azure':
print(m.version)
回答by Xavier Guihot
Starting Python 3.8, importlib.metadatacan be used as a replacement for pkg_resourcesto extract the version of third-party packages installed via tools such as pip:
开始Python 3.8,importlib.metadata可以用来替代pkg_resources提取通过工具安装的第三方软件包的版本,例如pip:
from importlib.metadata import version
version('wheel')
# '0.33.4'
回答by aastha
import sys
import matplotlib as plt
import pandas as pd
import sklearn as skl
import seaborn as sns
print(sys.version)
print(plt.__version__)
print(pd.__version__)
print(skl.__version__)
print(sns.__version__)
The above code shows versions of respective modules: Sample O/P:
上面的代码显示了各个模块的版本:示例 O/P:
3.7.1rc1 (v3.7.1rc1:2064bcf6ce, Sep 26 2018, 14:21:39) [MSC v.1914 32 bit (Intel)] 3.1.0 0.24.2 0.21.2 0.9.0 (sys shows version of python )
3.7.1rc1 (v3.7.1rc1:2064bcf6ce, Sep 26 2018, 14:21:39) [MSC v.1914 32 位(英特尔)] 3.1.0 0.24.2 0.21.2 0.9.0(系统显示 python 版本) )

