Installation
July 7, 2023 ยท View on GitHub
First, decide on values for the following configuration variables:
-
VCS: the version control system you use. Currently accepts "git". -
style: the style of version string to be produced. See Styles for details. Defaults to "pep440", which looks likeTAG[+DISTANCE.gSHORTHASH[.dirty]]. -
versionfile_source:A project-relative pathname into which the generated version strings should be written. This is usually a
_version.pynext to your project's main__init__.pyfile, so it can be imported at runtime. If your project usessrc/myproject/__init__.py, this should besrc/myproject/_version.py. This file should be checked in to your VCS as usual: the copy created below byversioneer installwill include code that parses expanded VCS keywords in generated tarballs. The 'build' and 'sdist' commands will replace it with a copy that has just the calculated version string.This must be set even if your project does not have any modules (and will therefore never import
_version.py), since "setup.py sdist" -based trees still need somewhere to record the pre-calculated version strings. Anywhere in the source tree should do. If there is a__init__.pynext to your_version.py, theversioneer installcommand (described below) will append some__version__-setting assignments, if they aren't already present. -
versionfile_build:Like
versionfile_source, but relative to the build directory instead of the source directory. These will differ when your setup.py uses 'package_dir='. If you havepackage_dir={'myproject': 'src/myproject'}, then you will probably haveversionfile_build='myproject/_version.py'andversionfile_source='src/myproject/_version.py'.If this is set to None, then
setup.py buildwill not attempt to rewrite any_version.pyin the built tree. If your project does not have any libraries (e.g. if it only builds a script), then you should useversionfile_build = None. To actually use the computed version string, yoursetup.pywill need to overridedistutils.command.build_scriptswith a subclass that explicitly inserts a copy ofversioneer.get_version()into your script file. Seetest/demoapp-script-only/setup.pyfor an example. -
tag_prefix:a string, like 'PROJECTNAME-', which appears at the start of all VCS tags. If your tags look like 'myproject-1.2.0', then you should use tag_prefix='myproject-'. If you use unprefixed tags like '1.2.0', this should be an empty string, using either
tag_prefix=ortag_prefix=''. -
parentdir_prefix:a optional string, frequently the same as tag_prefix, which appears at the start of all unpacked tarball filenames. If your tarball unpacks into 'myproject-1.2.0', this should be 'myproject-'. To disable this feature, just omit the field from your
setup.cfg.
This tool provides one script, named versioneer. That script has two modes:
- "install --vendor", which writes a copy of
versioneer.pyinto the current directory and runsversioneer.py setupto finish the installation. - "install --no-vendor", which attempts to run versioneer without installing a
copy into the current directory. This is an experimental mode intended to
work with
pyproject.toml.
Installation instructions
Common steps
To versioneer-enable your project:
-
1: Install versioneer with
pip install versioneer -
2: Modify your
pyproject.tomlor yoursetup.cfg, adding a section named[tool.versioneer]or[versioneer](respectively) and populating it with the configuration values you decided earlier (note that the option names are not case-sensitive):[tool.versioneer] VCS = "git" style = "pep440" versionfile_source = "src/myproject/_version.py" versionfile_build = "myproject/_version.py" tag_prefix = "" parentdir_prefix = "myproject-"[versioneer] VCS = git style = pep440 versionfile_source = src/myproject/_version.py versionfile_build = myproject/_version.py tag_prefix = parentdir_prefix = myproject- -
3: If using the EXPERIMENTAL non-vendored mode, add
versioneerto yourpyproject.toml:[build-system] requires = ["setuptools", "versioneer[toml]==0.29"] build-backend = "setuptools.build_meta"It is recommended to pin the version of Versioneer you installed with. The
[toml]extra is required if your configuration is alsopyproject.toml. -
4: Run
versioneer install --vendorORversioneer install --no-vendor. This will do the following:- copy
versioneer.pyinto the top of your source tree (vendored mode) - create
_version.pyin the right place (versionfile_source) - modify your
__init__.py(if one exists next to_version.py) to define__version__(by calling a function from_version.py)
versioneer installwill complain about any problems it finds with yoursetup.pyorsetup.cfg. Run it multiple times until you have fixed all the problems. - copy
-
5: add a
import versioneerto your setup.py, and add the following arguments to the setup() call:version=versioneer.get_version(), cmdclass=versioneer.get_cmdclass(),If your project uses a special
cmdclass, pass thatcmdclassas a parameter. For example:from numpy.distutils.core import numpy_cmdclass cmdclass=versioneer.get_cmdclass(numpy_cmdclass), -
6: commit these changes to your VCS. To make sure you won't forget,
versioneer installwill mark everything it touched for addition usinggit add. Don't forget to addsetup.pyandsetup.cfgtoo.
Post-Installation Usage
Once established, all uses of your tree from a VCS checkout should get the current version string. All generated tarballs should include an embedded version string (so users who unpack them will not need a VCS tool installed).
If you distribute your project through PyPI, then the release process should boil down to two steps:
- 1: git tag 1.0
- 2: python setup.py register sdist upload
If you distribute it through github (i.e. users use github to generate
tarballs with git archive), the process is:
- 1: git tag 1.0
- 2: git push; git push --tags
Versioneer will report "0+untagged.NUMCOMMITS.gHASH" until your tree has at least one tag in its history.