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

后端 未结 6 626
栀梦
栀梦 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:13

    the solution provided by spinus works if the python file does not import any additional python-library.

    However, if a python file imports an additional library, lets say numpy, the above code crashes as follows:

    :~/programs/python$ ./a.out myModule multiply 4 3
    Traceback (most recent call last):
      File "/home/a/programs/python/myModule.py", line 1, in 
        import numpy
    ImportError: No module named 'numpy'
    Failed to load "myModule"
    

    As a remark, the import of the python-library from C does not work:

    PyObject *pNumpy = PyUnicode_FromString("numpy");
    PyObject *pModuleA = PyImport_Import(pNumpy); 
    

    Does someone know how to call from C python-functions, which depend of some other python-libraries?

提交回复
热议问题