I am trying to use the `python_distribution` targe...
# general
q
I am trying to use the
python_distribution
target to package my project with a
pyproject.toml
file. Everything works but I can’t seem to set the
version
from the BUILD file. It forces me to fix the
version
field in the
pyproject.toml
itself. Example in thread.
Excerpt from BUILD file
Copy code
python_distribution(
    name="dist",
    dependencies=[":readme", ":pyproject", ":source", ":manifest"],
    provides=python_artifact(
        name="my-project",
        version="0.0.1",
    ),
    generate_setup=False,
)
Except from
pyproject.toml
file
Copy code
[build-system]
requires = ["setuptools", "setuptools-scm", "wheel"]
build-backend = "setuptools.build_meta"

[project]
classifiers = [
    "Development Status :: 5 - Production/Stable",
    "Environment :: Console",
    "Intended Audience :: Developers",
    "Intended Audience :: Science/Research",
    "Operating System :: POSIX :: Linux",
    "Programming Language :: Python :: 3.9",
    "Topic :: Scientific/Engineering",
]
name = "my-project"
version = "2024.3"
When I try to
pants package path/to/BUILD:dist
the generated package has the version
2024.3
instead of
0.0.1
from the BUILD file. If I try to remove the
version
field from the
pyproject.toml
file the command complains
Copy code
ValueError: invalid pyproject.toml config: `project`.
configuration error: `project` must contain ['version'] properties
Eventually, I would like to do some sort of custom automatically generated version numbers so I don’t want to fix them in the
pyproject.toml
file.
c
cc @happy-kitchen-89482
i
This worked for me:
the
project.toml
file:
Copy code
[project]
name = "oxygen"
dependencies = ["click"]
dynamic = ["version"]


[tool.setuptools.dynamic]
version = { "file" = "VERSION" }

[build-system]
requires = ["setuptools>=64", "setuptools_scm>=8", "wheel"]
build-backend = "setuptools.build_meta"
And the BUILD file:
Copy code
python_sources(name="lib", sources=["oxygen/**/*.py"])
python_requirements(source="pyproject.toml")

# pyproject.toml based distribution
resource(name="pyproject", source="pyproject.toml")

vcs_version(
    name="version",
    generate_to=str(build_file_dir() / "VERSION"),
    template="{version}",
    version_scheme="guess-next-dev",
    local_scheme="no-local-version",
)

python_distribution(
    name="dist",
    dependencies=[":pyproject", ":lib", ":version"],
    provides=python_artifact(),
    generate_setup=False,
)
👀 1
This will smartly pull version from Git
🙏 1
❤️ 1
q
Thanks so much for the example!
Hey @important-hydrogen-79200, a follow up question - this generates a package with version
0.1.dev64
. How can I specify the base version to bump from, say I wanted it to be
5.1
or something when I am on the
main
branch and
5.1.dev64
when I am on
dev
branch? Am I asking too much of setuptools-scm + the vcs_version target or is this possible? The setuptools-scm documentation does not clearly specify this.
i
AFAIK, this might not be possible using plain vcs_version as it’s a wrapper of setuptools-scm.
👍 1
h
Ah yes, the
version
in
python_artifact
is injected when Pants generates
setup.py
for you, but if you use your own
pyproject.toml
, that is treated as a static file, no munging of its fields is done
This is all going to be rethought in the New Python Backend