python2.7 - C++ 嵌入 Python 不能导入 lxml.html 的问题
PHP中文网
PHP中文网 2017-04-17 17:33:57
0
2
385

我在系统中安装了 Python2.7 和 lxml,通过命令行执行 import lxml.html 没有问题。

但是我在 C++ 中嵌入 Python,调用 PyRun_SimpleString("import lxml.html"); 就会提示下面的错误

我已经把 Python 安装目录下的 DLLs Lib python27.dll 都拷贝到了我 C++ 程序的目录下了,请问大神这是个什么情况?

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
阿神

The solution was found:

Steps to find the problem:

  1. I copied the python.exe in the installation directory to my directory, used python.exe to execute import lxml.html and found that there was no problem, that is what I wrote myself What is the difference between exe and python.exe? I guess it may be a CRT problem. import lxml.html 发现没问题,那就是我自己写的 exe 和 python.exe 有什么不同啦,猜测可能是 CRT 的问题。

  2. 打开 python.exe 的 manifest,发现依赖 <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>

  3. 打开 etree.pyd 的 manifest,发现没有依赖 CRT 的信息。

  4. 我的 exe 是用 VS2013 编写的,打开 manifest,也发现没有依赖 CRT 的信息。

原因猜测:

由于 eptree.pyd 没有依赖 CRT 的信息,它就使用主程序中的 CRT 依赖信息,在 python.exe 中找到了,所以加载成功了。在我的 exe 中没有找到,就悲剧了。

解决方案:

有两种方法解决这个问题:

  1. 不使用 VS2013 了,使用 VS2008 编译 exe,编译好的 Release 版本 exe 里面的 manifest 中有 CRT 依赖信息,和 python.exe 没有区别了。

  2. 还使用 VS2013编译 exe, 但是需要修改 etree.pyd 的 manifest,添加 CRT 的依赖信息 <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>

Open the manifest of python.exe and find the dependency <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken ="1fc8b3b9a1e18e3b"></assemblyIdentity>

Open the manifest of etree.pyd and find that there is no information that relies on CRT.

  1. My exe is written in VS2013. When I open the manifest, I also find that there is no information that relies on CRT.

  2. Reason guess:

Since eptree.pyd does not have CRT dependency information, it uses the CRT dependency information in the main program and found it in python.exe, so the loading was successful. It's a tragedy if it's not found in my exe. #🎜🎜# #🎜🎜##🎜🎜#Solution: #🎜🎜##🎜🎜# #🎜🎜#There are two ways to solve this problem: #🎜🎜# #🎜🎜# #🎜🎜##🎜🎜# Instead of using VS2013, use VS2008 to compile the exe. The manifest in the compiled Release version exe has CRT dependency information, which is no different from python.exe. #🎜🎜##🎜🎜# #🎜🎜##🎜🎜# Also use VS2013 to compile the exe, but you need to modify the manifest of etree.pyd and add CRT dependency information <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version= "9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>#🎜🎜##🎜🎜# #🎜🎜# #🎜🎜#Both methods can solve the problem! #🎜🎜# #🎜🎜##🎜🎜#a little doubt#🎜🎜##🎜🎜# #🎜🎜# #🎜🎜##🎜🎜# There is no CRT dependency information in the VS2013 manifest. How does it solve the problem of multi-version CRT conflicts? Or does VS2013 write this information to other locations? #🎜🎜##🎜🎜# #🎜🎜##🎜🎜#Does a DLL without CRT dependency information really use the CRT dependency information in the main program as guessed? #🎜🎜##🎜🎜# #🎜🎜#
伊谢尔伦

Call first

import sys
sys.path

Check if lxml is in the loading path of sys.path
etree can be loaded separately
You must first make sure whether etree can be loaded normally

Try
1) PyRun_String
2) PyImport_ImportModule and PySys_SetPath, the latter can set sys.path
3) If neither 1 nor 2 works, you may have to change lxml /html/__init__.py, remove the usage of relative references in it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template