I'm trying to call cythonized python code from a C++ main. When python code includes an import line, the runtime fails due to not finding the imported package.
The simplified example below includes 6 files:
- quacker.py - a file that is cytonized to pyd format (which is a windows dll)
- setup.py - the file used for cytonizing quacker.py - i.e. converting it to a pyd file.
- caller.pyx - a file the is converted to a header and a cpp file that is compiled and linked in the c++ application.
- main.cpp - the main file of the C++ application
- Quack.vcxproj - The Visual-Studio-2019 project file of the application
- main.py - python file with which the cythonized file runs OK.
The file quacker.py
import jpeg
def quack():
print("\n\n############## Quack! ###############\n\n")
this file is cytonized by running the following command:
python setup.py build_ext --force --inplace
with the following setup.py file:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('quacker.py' , compiler_directives={'language_level' : "3"}))
the caller.pyx file is:
from quacker import quack
cdef public void call_quack():
quack()
This file is converted to a cpp and h files using the command:
cython --cplus caller.pyx -3
and finally the C++ application main file main.cpp:
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#else
#include <python.h>
#endif
#include "caller.h"
int main()
{
int status = PyImport_AppendInittab("caller", PyInit_caller);
Py_Initialize();
PyObject* module = PyImport_ImportModule("caller");
call_quack();
Py_Finalize();
return 0;
}
running the compiled C++ application results with the following errors:
Traceback (most recent call last): File "caller.pyx", line 1, in init caller from quacker import quack File "quacker.py", line 2, in init quacker def quack(): ModuleNotFoundError: No module named 'jpeg' Exception ignored in: 'caller.call_quack' Traceback (most recent call last):
File "caller.pyx", line 1, in init caller from quacker import quack File "quacker.py", line 2, in init quacker def quack(): ModuleNotFoundError: No module named 'jpeg'
Running the quacker.*.pyd from the following main.py python script results with a successful execution.
from quacker import quack
quack()
So does running it from the C++ application after removing the importing of jpeg from quacker.py.
So how can I run cytonized files that import libreries from a C++ application?
thanks