pip 如何确定 python 包版本
How pip determine a python package version
当我使用 pip 从源代码安装包时,它会为包生成一个版本号,我可以使用 'pip show ' 查看该版本号。但是我找不到那个版本号是如何生成的,也无法从源代码中找到版本字符串。谁能告诉我版本是如何生成的?
pip
使用的版本号来自setup.py
(如果你pip install
一个文件、目录、repo等)and/or中的信息PyPI 索引(如果你 pip install
一个包名)。 (因为这两个必须相同,所以哪个并不重要。)
建议软件包在运行时将相同的字符串用作其顶级 module/package(s) 上的 __version__
属性,但这不是必需的,并非每个包裹都如此。
如果包没有公开它的版本,你真的没有办法得到它。 (好吧,除非你想通过 pip
数据试图找出哪个包拥有一个模块然后获取它的版本。)
这是一个例子:
在 bs4
(BeautifulSoup4) 的源代码中,setup.py
文件有这一行:
version = "4.3.2",
这是 pip
直接或间接使用的版本。
然后,在 bs4/__init__.py
里面,有这样一行:
__version__ = "4.3.2"
这意味着 Leonard Richardson 是一个听从建议的好人,所以我可以 import bs4; print(bs4.__version__)
并取回 pip show beautifulsoup4
给我的相同版本字符串。
但是,如您所见,它们是完全不同文件中的两个完全不同的字符串。如果他不友善,他们可能完全不同,或者第二个可能丢失,或者命名不同。
OpenStack 人员在您的 git
存储库中提出了一个名为 PBR that helps you manage version numbers. You can read the linked doc page for the full details, but the basic idea is that it either generates the whole version number for you out of git, or verifies your specified version number (in the metadata
section of setup.cfg
) and appends the dev
build number out of git. (This relies on you using Semantic Versioning 的漂亮库。)
对于更复杂的字符串,如4.0.0rc1
,通常在PKG-INFO
文件中手工编辑。如:
# cat ./<package-name>.egg-info/PKG-INFO
...
Version: 4.0.0rc1
...
这使得无法从任何 python 代码中获取它。
而不是在代码中指定版本号,工具 such as setuptools-scm
may use tags from version control. Sometimes the magic is not directly visible. For example PyScaffold uses it,但在项目的根文件夹 __init__.py
中可能只看到:
import pkg_resources
try:
__version__ = pkg_resources.get_distribution(__name__).version
except:
__version__ = "unknown"
例如,如果Git中的最高版本标记是6.10.0,那么pip install -e .
将生成一个本地版本号,例如6.10.0.post0.dev23+ngc376c3c
(c376c3c
是最后一次提交的短散列)或 6.10.0.post0.dev23+ngc376c3c.dirty
(如果它有未提交的更改)。
当我使用 pip 从源代码安装包时,它会为包生成一个版本号,我可以使用 'pip show ' 查看该版本号。但是我找不到那个版本号是如何生成的,也无法从源代码中找到版本字符串。谁能告诉我版本是如何生成的?
pip
使用的版本号来自setup.py
(如果你pip install
一个文件、目录、repo等)and/or中的信息PyPI 索引(如果你 pip install
一个包名)。 (因为这两个必须相同,所以哪个并不重要。)
建议软件包在运行时将相同的字符串用作其顶级 module/package(s) 上的 __version__
属性,但这不是必需的,并非每个包裹都如此。
如果包没有公开它的版本,你真的没有办法得到它。 (好吧,除非你想通过 pip
数据试图找出哪个包拥有一个模块然后获取它的版本。)
这是一个例子:
在 bs4
(BeautifulSoup4) 的源代码中,setup.py
文件有这一行:
version = "4.3.2",
这是 pip
直接或间接使用的版本。
然后,在 bs4/__init__.py
里面,有这样一行:
__version__ = "4.3.2"
这意味着 Leonard Richardson 是一个听从建议的好人,所以我可以 import bs4; print(bs4.__version__)
并取回 pip show beautifulsoup4
给我的相同版本字符串。
但是,如您所见,它们是完全不同文件中的两个完全不同的字符串。如果他不友善,他们可能完全不同,或者第二个可能丢失,或者命名不同。
OpenStack 人员在您的 git
存储库中提出了一个名为 PBR that helps you manage version numbers. You can read the linked doc page for the full details, but the basic idea is that it either generates the whole version number for you out of git, or verifies your specified version number (in the metadata
section of setup.cfg
) and appends the dev
build number out of git. (This relies on you using Semantic Versioning 的漂亮库。)
对于更复杂的字符串,如4.0.0rc1
,通常在PKG-INFO
文件中手工编辑。如:
# cat ./<package-name>.egg-info/PKG-INFO
...
Version: 4.0.0rc1
...
这使得无法从任何 python 代码中获取它。
而不是在代码中指定版本号,工具 such as setuptools-scm
may use tags from version control. Sometimes the magic is not directly visible. For example PyScaffold uses it,但在项目的根文件夹 __init__.py
中可能只看到:
import pkg_resources
try:
__version__ = pkg_resources.get_distribution(__name__).version
except:
__version__ = "unknown"
例如,如果Git中的最高版本标记是6.10.0,那么pip install -e .
将生成一个本地版本号,例如6.10.0.post0.dev23+ngc376c3c
(c376c3c
是最后一次提交的短散列)或 6.10.0.post0.dev23+ngc376c3c.dirty
(如果它有未提交的更改)。