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

后端 未结 6 615
栀梦
栀梦 2021-02-07 11:32

Hi I\'m trying to embed python (2.7) into C++ (g++ 4.8.2) and hence call a python function from C++. This is the basic code provided in python documentation for embedding:

6条回答
  •  时光说笑
    2021-02-07 12:14

    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).

提交回复
热议问题