我想在C++中調用foo。
如果我添加了from lxml import html,但效果很好,當我删除它
Test.py
import os
import sys
import requests
from lxml import html #it will cause failed
def foo():
host = "http://www.baidu.com"
s = requests.session()
res = s.get(host)
return res
C++代碼将在步驟PyImport_ImportModule失敗:
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject* pModule = PyImport_ImportModule("Test"); //failed
if (pModule == NULL || PyErr_Occurred())
{
PyErr_Print();
}
PyObject* pDict = PyModule_GetDict(pModule);
PyObject *pFunHi = PyDict_GetItemString(pDict, "foo");
PyObject *ret = PyObject_CallFunction(pFunHi,NULL);
Py_DECREF(pFunHi);
Py_Finalize();
錯誤味精:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\test\Test\Debug\Test.py", line 4, in
from lxml import html
File "E:\python27\lib\site-packages\lxml\html\__init__.py", line 54, in
from .. import etree
ImportError: DLL load failed: Unable to find the specified module。
如何使用lxml子產品正确?如果pModule == NULL
+0
你應該檢查'pModule == NULL'或者是否發生錯誤。如果有幫助,請參閱我最近的[回複](http://stackoverflow.com/questions/38745999/embedding-python-in-c-segmentation-fault/38791595#38791595)。 –
+0
您應該驗證lxml是否已正确安裝。 –