python3 -m build ... supply version number via command line argument

Viewed 657

I use python3 -m build to build my python package like documented in the official docs: Packaging Projects.

But I don't want to store my version number in the source code, because this creates too many useless changes in the git repository.

Is there a way to leave version empty in setup.cfg and provide the version via the python3 -m build command line?

I use the recommended pyproject.toml file:

[build-system]
requires = [
    "setuptools>=42",
    "wheel"
]
build-backend = "setuptools.build_meta"

Related question in the Python Forum: https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188

Related issue: https://github.com/pypa/build/issues/347

2 Answers

The closest I could get is with a command like the following for a setuptools build back-end:

python -m build \
--config-setting=--global-option=egg_info \
--config-setting=--global-option=--tag-build=SUFFIX

This adds a suffix to the version string.

So if you set the version string to 1 in setup.cfg and SUFFIX to .2.3 on the command line, you would get 1.2.3.

This is a hack but seems to work well enough. Tested with:

  • Python 3.6.9
  • build 0.6.0.post1
  • setuptools 58.0.2

No guarantee it will continue to work in the foreseeable future. For a longer term solution, maybe it might be worth keeping an eye on and/or weighing in this setuptools feature request.

References:

I found this solution:

# setup.py
from setuptools import setup
import os
setup(version=os.environ.get('BUILD_VERSION'))

Call build like this:

BUILD_VERSION=... python3 -m build
Related