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?
square
to the C++ code, addedsquare
to the existing__init__.py
(would have been easier if you either mentioned that, or used a function that already exists there). I createdcube.py
as you described, and fixed the import statement to readfrom ._core import square
to avoid aModuleNotFoundError
. | If it build a wheel (pip wheel .
), all the files are there as expected. If I install it, I can do things likefrom scikit_build_example import cube
, orfrom scikit_build_example.cube import cube
just fine, and call the function.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 dofrom scikit_build_example import cube
?from .cube import cube
and then add"cube"
to the list assigned to__all__
.