incremental icon indicating copy to clipboard operation
incremental copied to clipboard

A proper way to reference version number in setup.cfg/setup.py?

Open asher-pembroke opened this issue 2 years ago • 1 comments

As per README, one can import the version number from __init__.py, which makes it accessible after all packages have been installed. However, a problem arises when referencing my package's version number in my setup.cfg like so:

[metadata]
name = mypackage
version = attr: mypackage.__version__

Setuptools complains that my package's dependencies are not installed. This is because __init__.py contains import statements for other packages like numpy.

One workaround is to create a submodule within mypackage/ called "versioning":

cd mypackage
python -m incremental.update versioning --rc

Next, define mypackage/versioning/__init__.py like this:

# needed for setuptools to resolve appropriate version
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

from ._version import __version__
# the default package name will be "versioning", so override with this file's grandparent directory
__version__.package = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

# need a method that renders the package name
def _setuptools_version():  # type: () -> str
    return __version__.public()

Now you can reference the version number in setup.cfg/setup.py without importing your project's dependencies:

[metadata]
name = mypackage
version = attr: mypackage.versioning._setuptools_version

asher-pembroke avatar Oct 13 '21 19:10 asher-pembroke