Domanda

I have following code

#include <boost/python.hpp>

int main()
{
    Py_Initialize();
    namespace python = boost::python;
    try {
        python::object main = python::import("sample");
    } catch(...) {
        PyErr_Print();
        PyErr_Clear();
    }
}

I get following error:

ImportError: No module named sample

I put my sample.py at the same directory as this program.

È stato utile?

Soluzione

It's because python::import is not looking inside the current directory. I know two ways to solve it:

Set the PYTHONPATH to look inside your current directory (linux):

export PYTHONPATH=`pwd`:$PYTHONPATH

or...

Set the python search module path inside your code (also it provides a better explanation about the issue that you've found out): How does import work with Boost.Python from inside python files

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top