从 git 标签获取版本(通过 pbr) [英] Get version from git tags (through pbr)

查看:33
本文介绍了从 git 标签获取版本(通过 pbr)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 pbr 进行包装.它从 git 标签中获取版本并将其应用于 setup.py

I use pbr for packaging. It takes the version from git tags and applies that to setup.py

现在我还想在包中提供可用的版本.例如有一个 __version__ 属性.我可以为此使用 pbr 库吗?

Now I also want to have the version available inside the package. For instance to have a __version__ attribute. Can I use the pbr library for this?

还有另一个库:versioneer 也从 git 标签中提取版本,但是这会增加一个额外的要求.我更愿意从 pbr

There is another library: versioneer that also extracts the version from the git tags, but that would add an extra requirement. I would prefer to get this functionality from pbr

推荐答案

正确设置setuptoolspbr 后,这里有几种方法可以做到:

After properly setting up for setuptools and pbr, here are several ways to do it:

import pkg_resources  # part of setuptools

# I don't like this one because the version method is hidden
v1 = pkg_resources.require("my_package_name")[0].version
print('v1 {}'.format(v1))

# This is my favorite - the output without .version is just a longer string with
# both the package name, a space, and the version string
v2 = pkg_resources.get_distribution('my_package_name').version
print('v2 {}'.format(v2))

from pbr.version import VersionInfo

# This one seems to be slower, and with pyinstaller makes the exe a lot bigger
v3 = VersionInfo('my_package_name').release_string()
print('v3 {}'.format(v3))

# Update, new option as of Python 3.8 (credit: sinoroc)
# In Python 3.8, importlib.metadata is part of the stdlib,
# which removes run-time dependencies on `pbr` or `setuptools`
import importlib.metadata

__version__ = importlib.metadata.version('my_package_name')

如果你想从命令行,你可以这样做:

If you want it from the command line, you can do:

py setup.py --version

或者甚至从脚本中运行 setup.py 脚本,如果包将始终安装在本地:

Or even run the setup.py script from within a script, if the package will always be installed locally:

from subprocess import Popen, PIPE

(output, err) = Popen('py setup.py --version'.split(' '),
                      stdout=PIPE, stderr=PIPE, text=True).communicate()
if err: print('ERROR: "{}"'.format(err))
else: print('setup.py --version = {}'.format(output))

注意:有关使用子进程启动和读取标准输出的更多详细信息,请参阅此答案等,尤其是在旧版本的 Python(3.7 之前).

Note: See this answer for more details on using subprocess to launch and read stdout, etc., especially on older versions of Python (prior to 3.7).

然后您可以像这样将 __version__ 添加到您的包 __init__.py 中:

You can then add __version__ to your package __init__.py like this:

__all__ = (
    '__version__',
    'my_package_name'
)

# Or substitute a different method and assign the result to __version__
import pkg_resources  # part of setuptools

__version__ = pkg_resources.get_distribution("my_package_name").version

<小时>

一些其他问答可能有助于设置和有关如何更新版本和其他信息的详细信息,尤其是从您的 Git 存储库获取信息(来自标签、作者和更改日志信息的版本):


Some other Q&A that might help with setup and details on how to update the version and other info, especially if getting info from your Git repo (version from tags, Authors, and ChangeLog info):

这篇关于从 git 标签获取版本(通过 pbr)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆