终于解决”dynamic module does not define init function (initlibxml2)”问题 Finally resolved "dynamic module does not define init function (initlibxml2)"
Py2exe和libxml2真是一个让人又爱又恨的东西。 Py2exe and libxml2 is a people又爱又恨things. 特别和python结合起来,时不时就给你来点问题。 Combine special and python, tried to point the problem to you.
因为要发布到没有安装过python的机器上,所以使用了py2exe来打包应用程序。 Because it is distributed to the python had not been installed on the machine, to use the py2exe packaged applications. 以前一直都好好的,最近一次打包却出现了 Has been good before, most recently appeared in bundles
dynamic module does not define init function (initlibxml2) Dynamic module does not define init function (initlibxml2)
的错误,想想最近曾经更新过libxml2,于是恢复到旧的版本,还是不行。 Mistakes, think about recently updated libxml2, then return to the old version or not.
Google了一下,找到几个方法,但都是似懂非懂的。 Google had to find several ways, but they are the Shidongfeidong.
折腾半天,最近总算是弄明白了,原来还是py2exe的问题: Tossing about half a day, the last finally understood, or py2exe original question:
由于程序使用打包的方式,打包运行后和直接运行.py程序的区别是sys.path不同了,原来版本py2exe打包后 As the use of packaged procedures, and direct packing operation after operation. Py process is the difference between sys.path different, original version py2exe after packing
len(sys.path)=1,但现在可能是py2exe什么时候升级过,变成了len(sys.path)=2了,也就是说: Len (sys.path) = 1, but now may be time to upgrade py2exe what had become a len (sys.path) = 2, that is to say:
PYTHON: PYTHON:
sys . path = [ ‘E: \\ bot \\ src \\ dist’ , ‘E: \\ bot \\ src \\ dist \\ bot2.exe’ ] Sys. Path = [ 'E: \ \ bot \ \ src \ \ dist', 'E: \ \ bot \ \ src \ \ dist \ \ bot2.exe']
这样的话,每次加载libxml2的时候,都会先查找dist,再查找exe本身,而dist目录下正好包含了libxml2.dll,而这是一个非python化的动态库,当然就报错了 So, each time loading libxml2, will first try to find dist again find itself exe, and dist directory contains libxml2.dll precisely, and this is a non-dynamic of the python, of course, on the error
知道了原因,解决起来就方便了,在应用程序头上加上 Know the reasons, the solutions to the convenience, in the head with applications
PYTHON: PYTHON:
if len ( sys . path ) > 1 : If len (sys. Path)> 1:
sys . path . insert ( 0 , sys . path [ - 1 ] ) Sys. Path. Insert (0, sys. Path [- 1])
print sys . path Print sys. Path
将调用路径换一下就可以了。 Will call for a path to it.




