1

This might be a trivial one, but I kind of stuck here.

I have the following setup:

  • entity.cpp/.hpp: containing my Entity class definition and implementation.
  • entity_wrap.cpp: my boost python wrapper file which I compile to entity.so
  • entity_test.cpp: a test file

What I'd like to do in entity_test.cpp is the following:

Py_SetProgramName(argv[0]);
Py_Initialize();

...
Entity* entity = new Entity;
globals["entity"] = entity;

I now get the following exception:

TypeError: No to_python (by-value) converter found for C++ type: Entity

Which is obvious since I do not load the conversion definition of my types. I now tried to load the entity.so with globals["entity_module"] = import("entity"); but I ran into this exception:

ImportError: No module named entity

I can load the module from a python shell as expected.

My question now is: how do I load the converters defined in entity_wrap.cpp?


Solution

As eudoxos stated, I have to ensure that the module I want to load is in the sys.path:

globals["sys"] = import("sys");
exec("sys.path.append('/path/to/my/module')\n"
     "import entity", globals);

It now works like a charm. Apparently just using Py_SetProgramName(argv[0]); was not enough.

1 Answer 1

1

With boost::python::import; watch for sys.path though so that your module is found, you might want to add a call to

PyRun_SimpleString("import sys; sys.path.append('.');")

first. Well, you can do the import via PyRun_SimpleString as well then :-)

Another option: write the entity_test itself in python.

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

1 Comment

eudoxos: Thanks a lot, I got a similar solution at about the same time. I will update my question accordingly.

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.