为什么我必须在 python 源代码中嵌入代码版本,有什么实际原因吗? [英] Is there any practical reason why I must have a code version embedded in python source code?

查看:58
本文介绍了为什么我必须在 python 源代码中嵌入代码版本,有什么实际原因吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我必须在源代码中嵌入代码版本,有什么实际原因吗?显然我对 setup.py 感兴趣,但是还有一些使用__version__.通过嵌入源代码"我的意思是我必须将版本号作为文本写入文件中,而不是通过其他方式填充 python 字段.

Is there any practical reason why I must have a code version embedded in the source code? Explicitly I'm interested in setup.py but also some use __version__. By "embed in source code" I mean must I write the version number in as text in the file rather than populating python fields by other means.

在其他语言中,我已经设置了构建脚本以了解 git 标签,然后完全通过标签管理版本号.这意味着 repo 中永远不会有修改版本号"的提交.我所要做的就是标记新版本的提交并运行构建.

In other languages, I've set up the build scripts to be aware of git tags and then managed version numbers entirely through tags. This means that there is never a commit in the repo to "bump the version number". All I have to do is tag the commit for the new version and run the build.

作为一个理论上的例子,我可能会:

So as a theoretical example I might:

def _get_version()
    command = ['git', 'describe', '--tags', '--match' 'versions/[0-9]*.[0-9]*']
    result = subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf-8')
    return result.replace('-', '+', 1).replace('versions/', '')

setuptools.setup(
    name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
    version=_get_version(),
    author="Example Author",
    author_email="author@example.com",
    description="A small example package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

我不清楚这是否可行.setuptools.setup 的结果是否在上传到包存储库之前或由第三方下载时被解析?当然,git repo 只在上传前可用,其他人通过 pip 下载的包不可用.

What's not clear to me is whether or not this would work. Is the result of setuptools.setup parsed before uploading to a package repository or when it's downloaded by a third party? Of course, the git repo is only available before it's uploaded not when the package downloaded by someone else through pip.

同样,我认为如果应用于 __version__ 文件将完全无法填充,我是否正确?

Likewise, am I correct in thinking this would utterly fail to populate if applied to a __version__ file?

推荐答案

从技术上讲,您不必将版本嵌入到您的包中.将版本传递给 setuptools.setup() 将确保它是保存在包元数据中以供打包机制处理(例如 setuptools、pip、诗歌等).

Technically, you don't have to embed the version in your package. Passing the version to setuptools.setup() will ensure that it will be save in the package metadata for the packaging mechanism to handle (e.g. setuptools, pip, poetry, etc.).

将它包含在包中被认为是一种很好的做法,因为当您编写需要检查库版本的代码时它可能很有用(__version__dundler"是在 PEP 8 中首次提到并在PEP 396),但由于 Python 3.8 嵌入了 importlib.metadata:

It has been deemed is a good practice to also include it to the package as it could be useful when you write code which needs to check version of the library (the __version__ "dundler" is first mentioned in PEP 8 and clarified in PEP 396), but it might not be necessary anymore since Python 3.8 embed importlib.metadata:

>>> from importlib.metadata import version
>>> version("wheel")
'0.35.1'

所以您的解决方案可以正常工作,但我建议您使用 setuptools-scm相反,这将阻止您自己管理子流程.

So your solution would work fine, but I would recommend you to use setuptools-scm instead which will prevent you for managing the subprocess yourself.

这篇关于为什么我必须在 python 源代码中嵌入代码版本,有什么实际原因吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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