Cython - Importing Cython Modules And Merging Them Into Single Shared Object
Solution 1:
Problem 1 is that your module initialization function is failing. In Python 3 you need to test the output against NULL, and then print the error (if it occurred):
PyObject* m = PyInit_sam1();
if (m==NULL) {
cout << "Module initialization failed!\n";
if (PyErr_Occurred()) {
PyErr_Print();
}
}
(I'm not 100% sure what you do for Python2 since I think it doesn't return anything. Probably just PyErr_Occurred
). This tells you:
Traceback (most recent call last):
File "sam1.pyx", line 2, in init sam1 (sam1.cpp:1094)
import sam2
ModuleNotFoundError: No module named 'sam2'
This traceback hopefully gives you the clue you need - it's not finding sam2
. This is because the current directory isn't on the path by default for C api programs. You need to set it up so that it is - the following code goes after Py_Initialize
but before the module initialization:
PyObject* path = PySys_GetObject("path");
// append "." to the path
PyObject* result = PyObject_CallMethod(path,"append","(s)",".");
Py_XDECREF(result);
// don't decref path though - it's borrowed
It gets sys.path
and appends .
to it. After that everything works fine.
Note - if you call your program from a different directory then "." refers to the wrong place. If this is the case then you need to do something cleverer and work out the fill path to the directory, probably from argv[0]
.
Post a Comment for "Cython - Importing Cython Modules And Merging Them Into Single Shared Object"