[英]single-sourcing package version for setup.cfg Python projects
For traditional Python projects with a setup.py
, there are various ways of ensuring that the version string does not have to be repeated throughout the code base.对于带有
setup.py
的传统 Python 项目,有多种方法可以确保版本字符串不必在整个代码库中重复。 See PyPA's guide on "Single-sourcing the package version" for a list of recommendations.有关建议列表,请参阅PyPA 的“单一来源包版本”指南。
Many are trying to move away from setup.py
to setup.cfg
(probably under the influence of PEP517 and PEP518 ; setup.py
was mostly used declaratively anyway, and when there was logic in setup.py
, it was probably for the worse.) This means that most the suggestions won't work anymore since setup.cfg
cannot contain "code".许多人试图从
setup.py
转移到setup.cfg
(可能是在PEP517和PEP518的影响下;无论如何setup.py
主要以声明方式使用,当setup.py
中有逻辑时,情况可能会变得更糟。 ) 这意味着大多数建议将不再起作用,因为setup.cfg
不能包含“代码”。
How can I single-source the package version for Python projects that use setup.cfg
?如何为使用
setup.cfg
的 Python 项目提供包版本的单一来源?
There are a couple of ways to do this (see below for the project structure used in these examples):有几种方法可以做到这一点(有关这些示例中使用的项目结构,请参见下文):
1. 1.
setup.cfg
[metadata]
version = 1.2.3.dev4
src/my_top_level_package/__init__.py
import importlib.metadata
__version__ = importlib.metadata.version('MyProject')
2. 2.
setup.cfg
[metadata]
version = file: VERSION.txt
VERSION.txt
1.2.3.dev4
src/my_top_level_package/__init__.py
import importlib.metadata
__version__ = importlib.metadata.version('MyProject')
3. 3.
setup.cfg
[metadata]
version = attr: my_top_level_package.__version__
src/my_top_level_package/__init__.py
__version__ = '1.2.3.dev4'
And more...和更多...
There are probably other ways to do this, by playing with different combinatons.通过使用不同的组合,可能还有其他方法可以做到这一点。
References :参考资料:
Structure assumed in the previous examples is as follows...前面示例中假设的结构如下...
MyProject
├── setup.cfg
├── setup.py
└── src
└── my_top_level_package
└── __init__.py
setup.py
#!/usr/bin/env python3
import setuptools
if __name__ == '__main__':
setuptools.setup(
# see 'setup.cfg'
)
setup.cfg
[metadata]
name = MyProject
# See above for the value of 'version = ...'
[options]
package_dir =
= src
packages = find:
[options.packages.find]
where = src
$ cd path/to/MyProject
$ python3 setup.py --version
1.2.3.dev4
$ python3 -m pip install .
# ...
$ python3 -c 'import my_top_level_package; print(my_top_level_package.__version__)'
1.2.3.dev4
$ python3 -V
Python 3.6.9
$ python3 -m pip list
Package Version
------------- ----------
MyProject 1.2.3.dev4
pip 20.0.2
pkg-resources 0.0.0
setuptools 45.2.0
wheel 0.34.2
zipp 3.0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.