0

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.

1
  • When debugging import issues, it can be helpful to set the PYTHONVERBOSE environment variable to 2. This will cause Python to print where python is trying to import files. Commented Feb 7, 2014 at 21:03

1 Answer 1

5

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

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

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.