Text of discuss.python.org › t › avoid-having-version-number-in-source-code › 10188 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout.

Open the live page

Avoid having version number in source code - Packaging - Discussions on Python.org

Avoid having version number in source code Packaging guettli (Thomas Güttler) August 17, 2021, 7:49am 1 I like to release often. The downside: Many changes in the git repo are changes to the version number. How could I avoid this? I don’t need Semantic Versioning in this case. Something like YYYY.MM.DD-NNN would be enough. Do you have an idea how this could be done? pf_moore (Paul Moore) August 17, 2021, 7:52am 2 I don’t understand the question.

If you’re using setuptools, you can calculate the version number at build time in setup.py . What else do you need? 1 Like frostming (Frost Ming) August 17, 2021, 8:45am 3 IIUC, you don’t want to keep version number in source code. Then you can retrieve it from SCM(e.g. git tag). setuptools-scm enables it for setuptools PDM also has native support for this, simply specify version as version = {use_scm = true} 5 Likes fungi (fungi) August 17, 2021, 2:28pm 4 A similar solution is provided by pbr · PyPI (which also allows you to avoid keeping things like changelogs and author lists in your source tree).

Like SetupTools-SCM, PBR calculates version numbers from tags, and can predict upcoming version bumps from structured footers in commit messages in order to more accurately form dev versions. 1 Like hlovatt (Howard Lovatt) August 20, 2021, 5:06am 5 #SelfPromotion This package pip install tag2ver will update all your version numbers, tag, upload to remote (optional), and upload to PyPI (optional) in one hit. So it might take the pain of doing frequent releases away.

It does for me - that’s why I wrote it! dustin (Dustin Ingram) August 20, 2021, 11:46am 6 Since you seem to want to do something similar to CalVer, I’ll shamelessly self promote as well: calver · PyPI will let you generate a version based on the current date. 2 Likes guettli (Thomas Güttler) September 4, 2021, 4:34pm 7 @pf_moore thank you for this hint. I was thinking the old fashioned input-processing-output way.

I dreamed that there is way to pass the version number via an argument on the command line. Up to now I only use a setup.cfg, and don’t need a setup.py. But you are right. I could get the version number from somewhere in setup.py and then pass the version number to setup() . fungi (fungi) September 4, 2021, 6:35pm 8 […] I could get the version number from somewhere in setup.py and then pass the version number to setup() .

This is pretty much exactly how libs like PBR and Setuptools-SCM inject versions extracted from Git tags. guettli (Thomas Güttler) September 9, 2021, 7:02pm 9 I published my solution here: setuptools - python3 -m build ... supply version number via command line argument - Stack Overflow 1 Like fungi (fungi) September 9, 2021, 7:23pm 10 Yep, that looks very similar to PBR’s environment variable solution with PBR_VERSION completely overriding its usual version autodetection logic (but much simpler of course because you don’t have a separate and basically unnecessary setup_requires to deal with): https://docs.openstack.org/pbr/latest/user/packagers.html#versioning I looked in the SetupTools-SCM docs and couldn’t find a similar option for it though.

EpicWink (Laurie O) September 9, 2021, 11:32pm 11 SETUPTOOLS_SCM_PRETEND_VERSION ? fungi (fungi) September 9, 2021, 11:50pm 12 Aha, yep thanks! I was apparently staring right at it in the environment variables section of the readme and didn’t see it. So anyway, both PBR and SetupTools-SCM have options to override version numbers from an envvar instead of taking them from revision control tags, but of course as shown it’s trivial to roll your own version override from an envvar in a setup.py file too. guettli (Thomas Güttler) September 10, 2021, 9:13am 13 I created a issue at github: https://github.com/pypa/build/issues/347 Let’s wait and see if other people have the same issue.