我编写了使用模块lxml
的python测试代码。导入lxml模块导致PyImport_ImportModule失败
我想在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 <module>
from lxml import html
File "E:\python27\lib\site-packages\lxml\html\__init__.py", line 54, in <module>
from .. import etree
ImportError: DLL load failed: Unable to find the specified module。
如何使用lxml模块正确?如果pModule == NULL
你应该检查'pModule == NULL'或者是否发生错误。如果有帮助,请参阅我最近的[回复](http://stackoverflow.com/questions/38745999/embedding-python-in-c-segmentation-fault/38791595#38791595)。 –
您应该验证lxml是否已正确安装。 –