I want to create a Python 3 package called "mypack" that can be installed with pip. It has some c-extensions and some python code that calls those extensions. Here is my directory structure:
setup.py
mypack/__init__.py
mypack/mypack.py
foopkg/foo.cpp
The setup.py file has the following code:
from setuptools import setup, Extension
PACKAGE_NAME = 'mypack'
module = Extension('foo',
language = "c++",
sources = ['foopkg/foo.cpp'])
setup(name=PACKAGE_NAME,
version='1.0',
packages=[PACKAGE_NAME],
ext_package=PACKAGE_NAME,
ext_modules=[module],
include_package_data=True)
I adapted this code from another related question where the user wanted to import the extensions using something like mypack.xxx
, as I do. In the c-api extension, I have successfully compiled it and made it work as a stand-alone extension module, but I am having trouble incorporating it into a larger package. It defines two functions make_array
and print_array
. For brevity I removed the function code and just included the stuff that Python needs:
...
static PyMethodDef FooMethods[] = {
{ "make_array", make_array, METH_VARARGS, "Put number in array"},
{ "print_array", print_array, METH_VARARGS, "Print number from array"},
{ NULL, NULL, 0, NULL}
};
static struct PyModuleDef foomodule = {
PyModuleDef_HEAD_INIT,
"foo",
"Make/Print array",
-1,
FooMethods
};
PyMODINIT_FUNC PyInit_foo(void)
{
return PyModule_Create(&foomodule);
}
I want to be able to import this extension within the package to use it (this is mypack.py
):
import mypack.make_array
import mypack.print_array
def dostuff():
array = make_array(10)
print_array(array)
Lastly, my __init__.py
file contains from .mypack import dostuff
.
However, when I install with pip, and try to run a test script that imports mypack
it complains about the imports in mypack.py
, whether I use foo.xx
or mypack.xx
. I have built other packages with a nested structure that use code from other python files in the module using __init__.py
and using imports. But I am a bit baffled about how to do this with c-extensions.