If you choose to only use setup.py
and exclude pyproject.toml
and setup.cfg
, you can retrieve the version of your package using Python code in any way that you prefer. For instance, you can obtain the version by reading an environment variable (e.g. EXAMPLE_VERSION
) within the setup.py
script:
import os
import setuptools
from packaging.version import Version
# Try to read version string from env variable
# Default to 0.0.0.dev0 to allow for installs through `pip install -e .`
version_string = os.environ.get("EXAMPLE_VERSION", "0.0.0.dev0")
version = Version(version_string)
setuptools.setup(
name="package-name",
version=str(version),
description="description",
long_description="long_description",
install_requires=[],
)
Code is from this gist.
Run
EXAMPLE_VERSION="0.1.0" python setup.py bdist_wheel
to build the dist package_name-0.1.0-py3-none-any.whl
However, if you want the version to be available in the variable __version__
in the __init__.py
file of your module, you need to dynamically update the value of this variable when running the setup.py
script. Here is an example:
import os
import re
from pathlib import Path
import setuptools
from packaging.version import Version
init_file = Path(__file__).parent.absolute() / "src/my_module/__init__.py"
# Try to read version string from env variable
# Default to 0.0.0.dev0 to allow for installs through `pip install -e .`
version_string = os.environ.get("EXAMPLE_VERSION", "0.0.0.dev0")
version = Version(version_string)
print(f"### NEW_VERSION: {version} ###")
try:
# read __init__.py file
with open(init_file, "r") as f:
file_contents = f.read()
# replace __version__ = "0.0.0" string
new_version_string = f'__version__ = "{version}"'
new_contents = re.sub(r'^__version__ = ".+"$', new_version_string, file_contents)
# write content to __init__.py file again
with open(init_file, "w") as f:
f.write(new_contents)
setuptools.setup(
name="package-name",
package_dir={"": "src"},
packages=["my_module"],
version=str(version),
description="description",
long_description="long_description",
install_requires=[],
)
finally:
with open(init_file, "w") as f:
f.write(file_contents)
Running
EXAMPLE_VERSION="0.2.0" python setup.py bdist_wheel
will build the bdist_wheel with the version 0.2.0. Before that, the __version__
variable in the __init__.py
is updated. After executing the setuptools.setup
command, the content of the __init__.py
is restored. Please note that I'm assuming a src layout here.
I haven't played around with the pyproject.toml
or setup.cfg
config files, but restricting yourself to only using the setup.py
allows you to write code that does what you want.
pyproject.toml
before the sdist build? Then the user has that result pre-computed, and wheel building with Setuptools can stick to the intended primary purpose - compiling extensions and organizing package folders. – Tamekia