Python C api PyImport_importmodule fail when the file has an import statement

前端 未结 1 1164
暖寄归人
暖寄归人 2021-01-22 05:27

I tried to use the Python C api to call a function from python in C++, the test was successful.

But if I intend to import a module already importing other module, the Py

相关标签:
1条回答
  • 2021-01-22 05:51

    A function call to PySys_SetPath() completely overwrites the Python module path. The result is that your Python script test_dl cannot find Python system modules (in this case __future__) and throws an exception.

    What you need to do is to append the directory of your module to the system path instead. To do that, first query the existing value of the system path, and then add your path to it:

    PyObject *sys_path = PySys_GetObject("path");
    PyList_Append(sys_path, PyString_FromString("C:/Users/Mik/Documents/GitHub/youtube-dl"));
    
    0 讨论(0)
提交回复
热议问题