-1

I have a project with this file structure:

project/
|- ...
|- include/
   \- version.hpp
|- conanfile.py
|- pyproject.toml
|- setup.py
\- version.py
  • version.hpp is the only point where I keep the current version.
  • version.py programmatically parses the version from version.hpp.
  • Both conanfile.py and setup.py do from version import get_version, and use get_version() to get the current version.
    python -m pip install . has been working without issues until now.
  • Now, I have added pyproject.toml (because I needed it to generate wheels via cibuildwheel), and when I do pipx run cibuildwheel or python -m pip install ., I get a ModuleNotFoundError: No module named 'version'.

How can I make version.py visible from setup.py when using pyproject.toml?

[Update]

I was initially wondering if I had missed some basic configuration but, according to the comment and answer, this is not an obvious catch.

I would add this is not a proper Python project. It's a C++ project, with SWIG and some auto-generated Python code.

I could add a link to the GitHub project, in case you may find that useful for looking at the source code.

My current solution is ugly, but just works. I copy-pasted the get_version() code from version.py into setup.py.

1
  • 2
    Impossible to answer without knowing what you have in setup.py and pyproject.toml respectively. Having a module version outside your Python package hierarchy is terrible form, though - consider a __version__ in your package __init__ instead? Commented May 31, 2024 at 17:29

1 Answer 1

1

In pyproject.toml you can define version as a dynamic and have the version written to a file of your choice if you're using setuptools_scm

# pyproject.toml
[project]
name = "your-project-name"
dynamic = [
  "version",
]
...

[tool.setuptools_scm]
write_to = "src/project/__version__.py
# setup.cfg

[options]
package_dir =
  = src

[options.packages.find]
where = src
# setup.py
from setuptools import setup

setup(name="project")
# src/project/__init__.py

from .__version__ import version

__version__ = version

A lot of this is dependent on the tools your using and this answer is based off work I've had to do in my own project with the build tools I'm specifically using. It would be helpful to have more details in regards to the contents of python build files

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time and sample code, @drGrove!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.