0

I have C++ program written at my home directory(/home/sunil/test.cc)

My Python module is residing at following path :- (/projects/name/dir/subdir/test.py)

I am trying to import python module in C++ using following Python-C API PyImport_ImportModule function.

PyImport_ImportModule(/projects/name/dir/subdir/test.py)

I am giving full path as argument to the function as shown above but it doesn't give any compilation error but when i try to print any variable value from python module it gives segmentation fault issue.

Couple of questions here :-

  1. If it is not able to load the python module then how it is not giving any compilation error?

  2. How do i provide the python module name to load it from some relative path from my main C++ program?

test.py code

n = 10
d = {1:5,2:10}

test.cc code

#include <Python.h>   
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

void c_python_intf(long long int i1,const char* s1,long long int i2)
{
   PyObject *mymod = NULL;
   PyObject *testmod = NULL;
   PyObject *py_attr_num = NULL;
   PyObject *python_func,*args,*myResult,*myData,*myString,*myNum;
   const char* py_var;
   long long int myData_updated;
   string myString_updated;
   long long int myNum_updated;
   cout << "input to c function is " << i1 << "\t\t" << s1 << "\t\t" << i2 << endl ;
   PyGILState_STATE gstate;
   gstate = PyGILState_Ensure();  
   string st = "/projects/name/dir/subdir";
   char *path;
   path = st.c_str();
   PySys_SetPath(path);
   // testmod = PyImport_ImportModule(".projects.name.dir.subdir.py_module");
   // testmod = PyImport_ImportModule("/projects/name/dir/subdir/py_module");
   testmod = PyImport_ImportModule("py_module");
   py_attr_num = PyObject_GetAttrString(testmod, "n");
   PyObject_Print(py_attr_num,stdout,0);
   cout << stdout << endl;
   py_attr_num = PyObject_GetAttrString(testmod, "d");
   PyObject_Print(py_attr_num,stdout,0);
   cout << stdout << endl;  
 }

 static PyMethodDef myModule_methods[] = { 
       {NULL, NULL} 
 };

 extern "C" { 
     /* * Python calls this to let us initialize our module */ 
   void initpython_sim(void) { 
           (void) Py_InitModule("python_sim", myModule_methods); 
   }
 }

 int main(int argc, char** argv)
 {
    string mod_name;
    cout << "###########################################" << endl;
    cout << "Running C++ code" << endl;
    cout << "###########################################" << endl;

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    initpython_sim();
    const char* input_string = "00110100000010100100001000000000";
    c_python_intf(100000000000LL,input_string,5000000000LL);
    //Py_Main(argc, argv);
    Py_Finalize();

    return 0;
 }

Note :- Code has been updated to remove redundant code which is not blocking the issue specified here. Hence please ignore input arguments to c_python_intf function.

The problem is since the python module and C++ program is not in the same directory its not able to load the python module. By copying the python module into the same directory where C++ program is written, it works.

How to tackle with this problem if python module and C++ program is not in the same directory?

2
  • Insufficient info. Provide a minimal reproducible example. Commented Nov 22, 2016 at 13:31
  • Okay, here's the code. Now, where does the error happen (and what error)? Segmentation fault at py_attr_num = PyObject_GetAttrString(...)? If that's the case, the import must be failing, and you're not checking that. Commented Nov 22, 2016 at 15:18

2 Answers 2

1

the below code works for me,

in Linux use:

setenv("PYTHONPATH", ".", 1);

In Windows:

_putenv_s("PYTHONPATH", ".");

you can replace your address in instead of ".", which illustrate the active path

Sign up to request clarification or add additional context in comments.

Comments

1

PyImport_ImportModule is a direct counterpart to import and accepts the name of the module rather than file path. If the module to load is not on sys.path, you need to add the corresponding sys.path entry.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.