https://pantsbuild.org/ logo
r

rapid-bird-79300

05/02/2023, 5:34 PM
Hey Pants Team, curious if we can read version using importlib something like this
Copy code
VERSION = importlib.metadata.version("my_pkg")
When running with Pants environments we get
Copy code
E   importlib.metadata.PackageNotFoundError: my_pkg
I found some older thread that suggested to use python_distribution but it seems
version
needs to be hardcoded there but we would prefer to leave version in the
pyproject.toml
. Is there any other ways we can do that with pyproject.toml?
e

enough-analyst-54434

05/02/2023, 5:41 PM
Most build backbends support version being a dynamic field; i.e.: you can convert it to canonically come from a constant in a Python module in your project. With that, no shenanigans. Just import the value. What is your build backend?
r

rapid-bird-79300

05/02/2023, 5:48 PM
Yeah we we're also considering that but we really want to stick with the version in pyproject.toml as most devs are familiar with using just that file. We us poetry to manage our venv and project deps.
e

enough-analyst-54434

05/02/2023, 5:52 PM
Forcing your normal dev flow to build sdist metadata is not awesome in general, but if you insist your leading assertion is false I think. You should be able to use
python_distribution
without repeating the version via: https://www.pantsbuild.org/docs/reference-python_distribution#codegenerate_setupcode
👀 1
For completeness: for Poetry dynamic versioning it looks like you'd use this: https://pypi.org/project/poetry-version-plugin/
r

rapid-bird-79300

05/02/2023, 6:00 PM
oh that looks like what we need let me give that a shot
with generate_setup option it still requires I provide version
Copy code
pants.backend.python.goals.setup_py.InvalidSetupPyArgs: Missing a `version` kwarg in the `provides` field for tools/python/my-pkg:dist. See <https://www.pantsbuild.org/v2.14/docs/python-distributions>.
oh I had it set to true
I tried this
Copy code
python_distribution(
    name="dist",
    provides=python_artifact(
        name="my-pkg",
    ),
    generate_setup=False,
)
and added the dependency on the targets that call
importlib
but still seeing same error.
pyproject.toml
lives in same directory as the BUILD file target above
e

enough-analyst-54434

05/02/2023, 6:43 PM
Did you add a dependency on this new target? Only dependencies on *_source(s) targets are inferred.
6 Views