Calling multiple python functions from different directories

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);

    // Initialize the Python Interpreter
    Py_Initialize();

    //CALL FUNCTION FROM FOLDER 1:
    std::wstring pathWide = L"./Folder 1";
    PySys_SetPath(pathWide.c_str());

    // Build the name object
    pName = PyUnicode_FromString((char*)"python_function");
    // Load the module object
    pModule = PyImport_Import(pName);
    // pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);
    // pFunc is also a borrowed reference
    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;}

    // Clean up
    Py_DECREF(pFunc);
    Py_DECREF(pDict);
    Py_DECREF(pModule);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    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);

    // Initialize the Python Interpreter
    Py_Initialize();

    //CALL FUNCTION FROM FOLDER 1:
    std::wstring pathWide = L"./Folder 1";
    PySys_SetPath(pathWide.c_str());

    // Build the name object
    pName = PyUnicode_FromString((char*)"python_function");
    // Load the module object
    pModule = PyImport_Import(pName);
    // pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);
    // pFunc is also a borrowed reference
    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;}

    //CALL FUNCTION FROM FOLDER 2:
    pathWide = L"./Folder 2";
    PySys_SetPath(pathWide.c_str());

    // Build the name object
    pName = PyUnicode_FromString((char*)"python_function");
    // Load the module object
    pModule = PyImport_Import(pName);
    // pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);
    // pFunc is also a borrowed reference
    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;}

    // Clean up
    Py_DECREF(pFunc);
    Py_DECREF(pDict);
    Py_DECREF(pModule);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    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:

:

#0 0x7ffff79b16cb   PyModule_GetDict() (/usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0:??)
#1 0x4010e6 main() (/home/ben/Documents/Programming/Projects/PYTHON TEST/main.cpp:23)

, 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'
+4
1

EDIT: .

. , . - - .

:

  • .
  • , PySys_SetPath().
  • , NULL PyImport_Import(), , ( .
  • , NULL , .

Python2.7 ( char *, wchar *), ). , :

  • PySys_SetPath().
  • , .
  • , . , . PyImport_ReloadModule().
  • , . , . .

, :

  • std::string std::wstring ( Python 2.x).
  • PyImport_ReloadModule() .
  • , sys.path .
  • . PyErr_Print() .
+4

All Articles