Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 504
guettli Avatar asked Sep 20 '25 07:09

guettli


2 Answers

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
like image 150
guettli Avatar answered Sep 23 '25 13:09

guettli


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:

  • https://pypa-build.readthedocs.io/en/latest/index.html#python--m-build---config-setting
  • https://github.com/pypa/build/issues/328#issuecomment-877028239
  • https://setuptools.readthedocs.io/en/latest/userguide/distribution.html#tagging-and-daily-build-or-snapshot-releases
like image 40
sinoroc Avatar answered Sep 23 '25 11:09

sinoroc