1

I'm trying to build upon this amazing example: https://github.com/pybind/scikit_build_example

I basically want to figure out how to add more functions or classes that are purely python. I thought I would need to add them to src/scikit_build_example as separate *.py files. For instance, add a file called cube.py:

from _core import square

def cube(num):
    return num * square(num)

where I've defined an additional square function on the C++ side, as a part of the _core module.

But, when I do pip install and try to use it, I get

AttributeError: module 'scikit_build_example' has no attribute 'cube'

How am I supposed to do this?

4
  • 1
    So, I fetched the example, added implementation of square to the C++ code, added square to the existing __init__.py (would have been easier if you either mentioned that, or used a function that already exists there). I created cube.py as you described, and fixed the import statement to read from ._core import square to avoid a ModuleNotFoundError. | If it build a wheel (pip wheel .), all the files are there as expected. If I install it, I can do things like from scikit_build_example import cube, or from scikit_build_example.cube import cube just fine, and call the function.
    – Dan Mašek
    Sep 22 at 10:20
  • Ok, I see. Thank you for that. Sorry I forgot to add the info about square. So I'd forgotten the . in front of ._core, and can now do the import as you note. What would I need to add to __init__.py so that I can just do from scikit_build_example import cube? Sep 22 at 13:34
  • 1
    Do from .cube import cube and then add "cube" to the list assigned to __all__.
    – Dan Mašek
    Sep 22 at 14:12
  • @DanMašek Thank you! Sep 22 at 14:15

0

Your Answer

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

Browse other questions tagged or ask your own question.