【发布时间】:2016-08-12 16:13:54
【问题描述】:
我编写了使用模块lxml的python测试代码。
我想在 C++ 中调用 foo。
如果我添加from lxml import html,它将在步骤PyImport_ImportModule 中失败,但当我删除它时效果很好
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++代码:
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或是否发生错误。如果有帮助,请查看我最近的answer。 -
您应该验证 lxml 是否已正确安装。
标签: python html c++ import lxml