使用py2exe教程时LoadLibrary(pythondll)失败错误

4 投票
2 回答
10158 浏览
提问于 2025-04-16 22:14

我正在尝试使用py2exe,现在遇到了一些麻烦,特别是在构建py2exe自带的示例和教程时。我运行了setup.py,过程很顺利,但当我尝试运行生成的exe文件时,出现了“LoadLibrary(pythondll) failed”的错误。我没有移动exe文件,它仍在dist目录下,而且我看到python27.dll也在那个dist目录里。有人知道可能发生了什么吗?

顺便提一下,我是在Windows 7上运行32位的Python 2.7,并且使用的是对应的32位Python 2.7的py2exe。

谢谢

我的test.py文件里只包含了:

print "test"

这是我根据Kirk写的内容整理的setup.py:

from distutils.core import setup
import py2exe
import sys
from glob import glob

project_folder = r'C:\\Python27\\Lib\site-packages\\py2exe\\samples\\test\\'
data_files = [
        ("dlls", glob(project_folder + r'dlls\\*.dll'))                  
        ,("pyds", glob(project_folder + r'pyds\\*.pyd'))  
         ]
options = { }

setup(
name='test'
,options = options
,zipfile = None
,data_files=data_files
,console=['test.py']
)

2 个回答

1

我知道这个问题已经很老了,但我遇到了类似的情况。 我之前卸载了64位的Python和py2exe,想换成32位的版本。 在我这样做之后,总是出现错误。 后来,我把项目中的dist和build文件夹删掉了,接下来的构建就成功了。

1

你需要特别包含 python27.dll 这个文件。如果你要包含多个文件,可以使用 glob 和一个数据文件数组,像下面这样做,这样可以在使用 py2exe 时得到更好的效果。举个例子,先创建一个 Dll 文件夹,把 python27.dll 放进去。

from distutils.core import setup
import py2exe
import sys
from glob import glob
data_files = [
        ("Stuff", glob(r'C:\projectfolder\Stuff\*.*')) 
        ,("dlls", glob(r'C:\projectfolder\dlls\*.dll'))                  
        ,("pyds", glob(r'C:\projectfolder\pyds\*.pyd'))  
         ]
options = { }

setup(
name='ProjectName'
,options = options
,zipfile = None
,data_files=data_files
,console=['projectname.py']
)

撰写回答