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 的问题。
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.
My exe is written in VS2013. When I open the manifest, I also find that there is no information that relies on CRT.
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? #🎜🎜##🎜🎜#
#🎜🎜#
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
The solution was found:
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 的问题。打开 python.exe 的 manifest,发现依赖
<assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
打开 etree.pyd 的 manifest,发现没有依赖 CRT 的信息。
我的 exe 是用 VS2013 编写的,打开 manifest,也发现没有依赖 CRT 的信息。
由于 eptree.pyd 没有依赖 CRT 的信息,它就使用主程序中的 CRT 依赖信息,在 python.exe 中找到了,所以加载成功了。在我的 exe 中没有找到,就悲剧了。
有两种方法解决这个问题:
不使用 VS2013 了,使用 VS2008 编译 exe,编译好的 Release 版本 exe 里面的 manifest 中有 CRT 依赖信息,和 python.exe 没有区别了。
还使用 VS2013编译 exe, 但是需要修改 etree.pyd 的 manifest,添加 CRT 的依赖信息
<assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
<assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken ="1fc8b3b9a1e18e3b"></assemblyIdentity>
Reason guess:
<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
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