# Avoid having version number in source code

**URL:** <https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188>\
**Category:** Packaging\
**Created:** [August 17, 2021, 7:49am UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188 "2021-08-17T07:49:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![guettli](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guettli/32/3601_2.png) [@guettli](https://discuss.python.org/u/guettli)\
**Post date:** [August 17, 2021, 7:49am UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/1 "2021-08-17T07:49:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [August 17, 2021, 7:52am UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/2 "2021-08-17T07:52:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![frostming](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/frostming/32/326_2.png) [@frostming](https://discuss.python.org/u/frostming)\
**Post date:** [August 17, 2021, 8:45am UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/3 "2021-08-17T08:45:08Z")

</div>

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

> **[setuptools-scm](https://pypi.org/project/setuptools-scm/)**
>
> the blessed package to manage your versions by scm tags

PDM also has native support for this, simply specify version as `version = {use_scm = true}`

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [August 17, 2021, 2:28pm UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/4 "2021-08-17T14:28:12Z")

</div>

A similar solution is provided by [pbr · PyPI](https://pypi.org/project/pbr)  
(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.

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hlovatt/32/36838_2.png) [@hlovatt](https://discuss.python.org/u/hlovatt)\
**Post date:** [August 20, 2021, 5:06am UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/5 "2021-08-20T05:06:26Z")

</div>

#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!

---

<div class="post-metadata">

**Author:** ![dustin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dustin/32/149_2.png) [@dustin](https://discuss.python.org/u/dustin)\
**Post date:** [August 20, 2021, 11:46am UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/6 "2021-08-20T11:46:22Z")

</div>

Since you seem to want to do something similar to CalVer, I’ll shamelessly self promote as well: [calver · PyPI](https://pypi.org/project/calver/) will let you generate a version based on the current date.

---

<div class="post-metadata">

**Author:** ![guettli](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guettli/32/3601_2.png) [@guettli](https://discuss.python.org/u/guettli)\
**Post date:** [September 4, 2021, 4:34pm UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/7 "2021-09-04T16:34:25Z")

</div>

@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()`.

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [September 4, 2021, 6:35pm UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/8 "2021-09-04T18:35:06Z")

</div>

[…]

> 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.

---

<div class="post-metadata">

**Author:** ![guettli](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guettli/32/3601_2.png) [@guettli](https://discuss.python.org/u/guettli)\
**Post date:** [September 9, 2021, 7:02pm UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/9 "2021-09-09T19:02:20Z")

</div>

> [@fungi](#):
>
> PBR

I published my solution here: [setuptools - python3 -m build ... supply version number via command line argument - Stack Overflow](https://stackoverflow.com/a/69123462/633961)

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [September 9, 2021, 7:23pm UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/10 "2021-09-09T19:23:50Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [September 9, 2021, 11:32pm UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/11 "2021-09-09T23:32:20Z")

</div>

> [@fungi](#):
>
> I looked in the SetupTools-SCM docs and couldn’t find a similar  
> option for it though.

`SETUPTOOLS_SCM_PRETEND_VERSION`?

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [September 9, 2021, 11:50pm UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/12 "2021-09-09T23:50:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![guettli](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guettli/32/3601_2.png) [@guettli](https://discuss.python.org/u/guettli)\
**Post date:** [September 10, 2021, 9:13am UTC](https://discuss.python.org/t/avoid-having-version-number-in-source-code/10188/13 "2021-09-10T09:13:25Z")

</div>

I created a issue at github: [https://github.com/pypa/build/issues/347](https://github.com/pypa/build/issues/347)

Let’s wait and see if other people have the same issue.
