简体   繁体   English

pip如何确定python软件包版本

[英]How pip determine a python package version

When I use pip to install a package from source, it will generates a version number for the package which I can see using 'pip show '. 当我使用pip从源代码安装软件包时,它将为该软件包生成一个版本号,使用'pip show'可以看到该版本号。 But I can't find out how that version number is generated and I can't find the version string from the source code. 但是我找不到该版本号的生成方式,也无法从源代码中找到版本字符串。 Can someone tell me how the version is generated? 有人可以告诉我该版本是如何生成的吗?

The version number that pip uses comes from the setup.py (if you pip install a file, directory, repo, etc.) and/or the information in the PyPI index (if you pip install a package name). pip使用的版本号来自setup.py (如果您pip install文件,目录,存储库等)和/或PyPI索引中的信息(如果pip install软件包名称)。 (Since these two must be identical, it doesn't really matter which.) (由于这两个必须相同,所以实际上无关紧要。)

It's recommended that packages make the same string available as a __version__ attribute on their top-level module/package(s) at runtime that they put in their setup, but that isn't required, and not every package does. 建议软件包在运行时为其顶级模块/软件包提供与设置中相同的字符串作为__version__属性,但这不是必需的,并不是每个软件包都这样做。

And if the package doesn't expose its version, there's really no way for you to get it. 而且,如果该软件包未公开其版本,则实际上您没有办法获得它。 (Well, unless you want to grub through the pip data trying to figure out which package owns a module and then get its version.) (好吧,除非您想翻阅pip数据以试图找出哪个软件包拥有一个模块,然后获取其版本。)


Here's an example: 这是一个例子:

In the source code for bs4 (BeautifulSoup4), the setup.py file has this line: bs4 (BeautifulSoup4)的源代码中, setup.py文件包含以下行:

version = "4.3.2",

That's the version that's used, directly or indirectly, by pip . 那是pip直接或间接使用的版本。

Then, inside bs4/__init__.py , there's this line: 然后,在bs4/__init__.py内部,有以下行:

__version__ = "4.3.2"

That means that Leonard Richardson is a nice guy who follows the recommendations, so I can import bs4; print(bs4.__version__) 这意味着伦纳德·理查森(Leonard Richardson)是一个遵循建议的好人,因此我可以import bs4; print(bs4.__version__) import bs4; print(bs4.__version__) and get back the same version string that pip show beautifulsoup4 gives me. import bs4; print(bs4.__version__)并返回pip show beautifulsoup4给我的相同版本字符串。

But, as you can see, they're two completely different strings in completely different files. 但是,正如您所看到的,它们是完全不同的文件中的两个完全不同的字符串。 If he wasn't nice, they could be totally different, or the second one could be missing, or named something different. 如果他不好,它们可能会完全不同,或者第二个可能会丢失,或者命名不同。


The OpenStack people came up with a nifty library named PBR that helps you manage version numbers. OpenStack人员想到了一个名为PBR的漂亮库,可帮助您管理版本号。 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. 您可以阅读链接的文档页面以获取全部详细信息,但是基本思想是,它可以从git中为您生成完整的版本号,或者验证您指定的版本号(在setup.cfgmetadata部分中)并附加git中的dev版本号。 (This relies on you using Semantic Versioning in your git repo.) (这取决于您在git repo中使用语义版本控制 。)

For more complicated strings such as 4.0.0rc1 , they are usually hand edited in the PKG-INFO file. 对于更复杂的字符串(例如4.0.0rc1 ,通常可以在PKG-INFO文件中手动对其进行编辑。 Such as: 如:

# cat ./<package-name>.egg-info/PKG-INFO 
...
Version: 4.0.0rc1
...

This make it unfeasible to obtain it from within any python code. 这使得从任何python代码中获取它都不可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM