Setuptools extension for CalVer package versions https://pypi.org/p/calver
Find a file
2025-10-20 16:34:36 -04:00
.github/workflows Support running tests from source distribution (#22) 2025-04-02 14:45:28 +00:00
src/calver Support SOURCE_DATE_EPOCH for reproducible builds (#15) 2025-03-31 12:33:33 -04:00
tests Fix DeprecationWarning for datetime.datetime.utcnow() (#27) 2025-10-20 16:34:36 -04:00
.flake8 Initial commit 2020-06-17 21:47:25 -05:00
.gitignore Initial commit 2020-06-17 21:47:25 -05:00
LICENSE Initial commit 2020-06-17 21:47:25 -05:00
MANIFEST.in test: Delete SOURCE_DATE_EPOCH envvar if already present for robustness (#24) 2025-04-17 09:43:33 -04:00
pyproject.toml Update build backend (#21) 2025-04-02 09:57:56 -04:00
README.md Support SOURCE_DATE_EPOCH for reproducible builds (#15) 2025-03-31 12:33:33 -04:00
setup.py Switch to pyproject.toml as much as possible (#18) 2025-04-01 10:11:04 -04:00
tox.ini Drop 3.7 and 3.8 (#16) 2025-03-31 10:15:12 -04:00

CalVer

The calver package is a setuptools extension for automatically defining your Python package version as a calendar version.

Usage

First, ensure calver is present during the project's build step by specifying it as one of the build requirements:

pyproject.toml:

[build-system]
requires = ["setuptools>=42", "calver"]

To enable generating the version automatically based on the date, add the following to setup.py:

setup.py:

from setuptools import setup

setup(
    ...
    use_calver=True,
    setup_requires=['calver'],
    ...
)

You can test that it is working with:

$ python setup.py --version
2020.6.16

Configuration

By default, when setting use_calver=True, it uses the following to generate the version string:

>>> import datetime
>>> datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y.%m.%d")
2020.6.16

You can override the format string by passing it instead of True:

setup.py:

from setuptools import setup

setup(
    ...
    use_calver="%Y.%m.%d.%H.%M",
    setup_requires=['calver'],
    ...
)

You can override the current date/time by passing the environment variable SOURCE_DATE_EPOCH, which should be a Unix timestamp in seconds. This is useful for reproducible builds (see https://reproducible-builds.org/docs/source-date-epoch/):

env SOURCE_DATE_EPOCH=1743428011000 python setup.py --version

You can override this entirely by passing a callable instead, which will be called with no arguments at build time:

setup.py:

import datetime
from setuptools import setup

def long_now_version():
    now = datetime.datetime.now(tz=datetime.timezone.utc)
    return now.strftime("%Y").zfill(5) + "." + now.strftime("%m.%d")

setup(
    ...
    use_calver=long_now_version,
    setup_requires=['calver'],
    ...
)