Python Embedding in C++ : ImportError: No module named pyfunction

Try this one:

 $ PYTHONPATH=. ./call_function pyfunction multiply 2 3

if this won't work, try to make __init__.py file in this directory and try again.

UPDATE:

I think that PYTHONPATH is temporary solution, to test stuff. If you want to have a directory when all your embedded modules lives you have to put in your embedded interpreter something equilevant to this:

import sys
sys.path.insert(0, "./path/to/your/modules/")

You can do it probably in python in your interpreter or on C level. This will add search path in very similar manner as PYTHONPATH but it is more persistant and elegant (IMHO).


You can also try to include these code to your c program

Py_Initialize();
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));

Learn from Here


Hi to all those facing the same problem, I found the solution! setenv() is a function defined in stdlib.h which sets the environment variable. Just have to run it!

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

for more info on setenv:

$ man setenv

All the best :) Also, thanks to @spinus


Put the following in the C/C++ code, just after Py_Initialize();

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

Tags:

Python

C++