I have a code that will go to the directory (folder 1 for demo purposes) and then call the function with the name functionin the file python_function.py. The code is as follows:
#include <Python.h>
#include <string>
#include <iostream>
int main()
{
PyObject *pName, *pModule, *pDict, *pFunc;
setenv("PYTHONDONTWRITEBYTECODE", " ", 1);
Py_Initialize();
std::wstring pathWide = L"./Folder 1";
PySys_SetPath(pathWide.c_str());
pName = PyUnicode_FromString((char*)"python_function");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, (char*)"function");
if (pFunc != NULL)
{
if (PyCallable_Check(pFunc))
{
PyObject *pResult;
pResult = PyObject_CallFunction(pFunc, "");
Py_DECREF(pResult);
}
else {PyErr_Print();}
}
else {std::cout << "pFunc is NULL!" << std::endl;}
Py_DECREF(pFunc);
Py_DECREF(pDict);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
return 0;
}
This code compiles and works fine on my system, but as soon as I want to call another function in the second directory called Folder 2, I get the following error: Segmentation Fault (core dumped). This is the code:
#include <Python.h>
#include <string>
#include <iostream>
int main()
{
PyObject *pName, *pModule, *pDict, *pFunc;
setenv("PYTHONDONTWRITEBYTECODE", " ", 1);
Py_Initialize();
std::wstring pathWide = L"./Folder 1";
PySys_SetPath(pathWide.c_str());
pName = PyUnicode_FromString((char*)"python_function");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, (char*)"function");
if (pFunc != NULL)
{
if (PyCallable_Check(pFunc))
{
PyObject *pResult;
pResult = PyObject_CallFunction(pFunc, "");
Py_DECREF(pResult);
}
else {PyErr_Print();}
}
else {std::cout << "pFunc is NULL!" << std::endl;}
pathWide = L"./Folder 2";
PySys_SetPath(pathWide.c_str());
pName = PyUnicode_FromString((char*)"python_function");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, (char*)"function");
if (pFunc != NULL)
{
if (PyCallable_Check(pFunc))
{
PyObject *pResult;
pResult = PyObject_CallFunction(pFunc, "");
Py_DECREF(pResult);
}
else {PyErr_Print();}
}
else {std::cout << "pFunc is NULL!" << std::endl;}
Py_DECREF(pFunc);
Py_DECREF(pDict);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
return 0;
}
The error occurs after I call the first function, so it seems that it does not change directories or something like that. I am using Ubuntu and I have python 3.4
I tried other directory change methods, not only PySys_SetPath, but alsosetenv("PYTHONPATH", path, 1);
. , , , .
EDIT:
:
, debug , 23, 23 ,
:
PyImport_Import() PyImport_ReloadModule(), , :
ImportError: No module named 'imp'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
if not enabled():
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
import re
ImportError: No module named 're'
Original exception was:
ImportError: No module named 'imp'