October 21, 2024
Chicago 12, Melborne City, USA
python

Python package with python and C components


I’m writing a python project, which have python and C-python components. And I want to use pyproject.toml. I have such file structure:

./myproject
|
|- pyproject.toml
|
\-src
   |
   |- myproject
   |      |
   |      ...some python code...
   |
   \- c_modules
          |
          |- compile_c_modules.py
          |
          |- module1
          |     |
          |    ... some c code
          |
          \- module2
                |
               ... some c code

File compile_c_modules.py contains a class for building Extension

from setuptools import Extension
from setuptools.command.build_py import build_py as _build_py

class build_py(_build_py):
    def run(self):
        self.run_command("build_ext")
        return super().run()

    def initialize_options(self):
        super().initialize_options()
        if self.distribution.ext_modules == None:
            self.distribution.ext_modules = []

        module1 = Extension(name="myproject.module1",
                           sources=[
                              ....
                           ],
                           include_dirs=[
                              ....
                           ])
       self.distribution.ext_modules.append(module1)
       ....

And I’m not sure, how exactly should I write pyproject.toml for this.

I’ve tried

[tool.setuptools.package-dir]
"myproject" = "src/myproject"

[tool.setuptools.cmdclass]
build_py = "src.c_modules.compile_c_modules.build_py"

And it even runs locally, but generates wheel file without correct arch field, but the same actions on github CI make and error

  File "/tmp/build-env-l_9yf2xz/lib/python3.12/site-packages/setuptools/config/expand.py", line 192, in _find_spec
    spec = spec or importlib.util.find_spec(module_name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib.util>", line 91, in find_spec
ModuleNotFoundError: No module named 'src'
ERROR Backend subprocess exited when trying to invoke get_requires_for_build_wheel



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video