1

File structure:

Foo/
     list.so
     main.cpp
     list.cpp
     boost_wrapper.cpp

main.cpp code:

#include <Python.h>
#include "list.cpp"


int main(int argc, char *argv[]){


    PyObject *pimport;
    pimport=PyString_FromString("list");
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PyImport_Import(pimport);
    /*PyRun_SimpleString("l=list.LinkedList()");
    PyRun_SimpleString("l.insert(\"N\", 9)");
    PyRun_SimpleString("l.display()");*/
    Py_Finalize();      
}

ERROR:

ImportError: No module named list

However if I run python from bash, I'm able to successfully import the module and use all functions defined. I've also tried to import using just PyRun_SimpleString with no avail.

I suspect the current working directory is invisible to the Python Interpreter called by Py_Initialize().

Nihal Harish
  • 980
  • 5
  • 13
  • 31
  • Why are you including `list.cpp` into `main.cpp`? How did you create `list.so`? – user4815162342 Jul 04 '14 at 06:39
  • list.cpp contains the Linked List definition and functions. list.so was created after writing a boost::python wrapper for list.cpp And yes, import list works in IDLE, when I run it from the same directory. – Nihal Harish Jul 04 '14 at 06:42
  • Ok, I have a working solution; but I'm sure this is the worst implementation. [solution](http://paste.ubuntu.com/7745988/) – Nihal Harish Jul 04 '14 at 08:08
  • Maybe you need to prepend `.` to `sys.path`, after which `PyImport_Import` will work. – user4815162342 Jul 04 '14 at 08:57

1 Answers1

0

Add these lines after Py_Initialize(); to append your PYTHONPATH

PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
abyesilyurt
  • 399
  • 3
  • 13