I have PythonCode.py:
import os, ctypes
print "ctypes are imported"
And CCode.c:
...
PySys_SetPath(path_to_PythonCode);
PyObject *pModule = PyImport_ImportModule("PythonCode");
if (!pModule) {
PyErr_Print();
return;
}
...
PyErr_Print()
prints:
Traceback (most recent call last): File ".../PythonCode.py", line 1,
in <module> import os, ctypes
ImportError: No module named ctypes
Executing Python code in terminal shows that ctypes do exist:
$ python --version
Python 2.7.1
$ python PythonCode.py
ctypes are imported
Why Python/C API cannot import ctypes?
sys.path
list with a single path. Instead you can append or insert the new path:PyObject *sysPath = PySys_GetObject("path"); PyObject *path = PyString_FromString(path_to_PythonCode); PyList_Append(sysPath, path); // or PyList_Insert(sysPath, 0, path);
(needs error handling).