I am using PyBind11 to make a Python project.
My directory structure looks a like this:
./
my_pkg/
__init__.py
func1.py
func2.py
My C++ code looks like this:
int myfunc(){
return 1;
}
PYBIND11_PLUGIN(cppmodule) {
py::module m("cppmodule", "My cpp module");
m.def("myfunc",&myfunc,"This does stuff");
return m.ptr();
}
And my setup.py
looks like this:
from setuptools import setup, Extension
import glob
ext_modules = [
Extension(
"cppmodule",
glob.glob('src/*.cpp'),
include_dirs = ['lib/include', 'lib/pybind11/'],
language = 'c++',
extra_compile_args = ['-std=c++17'],
define_macros = [('DOCTEST_CONFIG_DISABLE',None)]
)
]
setup(name = 'bob',
version = '0.1',
description = 'A package about shrimp',
url = 'http://github.com/shrimp',
author = 'Bob',
author_email = '',
license = 'MIT',
ext_modules = ext_modules
)
Now, if I runn
python setup.py install
everything compiles.
But here's the odd part, later, I can run import cppmodule
but not import bob
. Or, with other fiddling, sometimes I can run both.
What I have not figured out how to do, but what I would like to do, is to have the C++ code incorporated into the bob
module the same way func1
and func2
will be, so that I can type bob.myfunc()
in Python.
How can I do this?
The answer was to modify the code in setup.py
to look like:
from setuptools import setup, Extension, find_packages
setup(name = 'bob',
version = '0.1',
description = 'A package about shrimp',
url = 'http://github.com/shrimp',
author = 'Bob',
author_email = '',
license = 'MIT',
packages = find_packages(),
ext_modules = ext_modules
)