本文译自Py2exe官网 http://www.py2exe.org/index.cgi/Py2exeAndWin32ui
问题
一个包含以下语句的 python 程序:
import win32ui
在exe 模式下产生一个错误 (也就是说,在使用 py2exe 完成编译后):
Traceback (most recent call last): File "test_win32ui.py", line 5, in <module> import win32ui File "win32ui.pyo", line 12, in <module> File "win32ui.pyo", line 10, in __load ImportError: DLL load failed: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.
解释
win32ui 需要 MFC (Microsoft Foundation Classes) DLLs 才能运行 .exe 模式。
解决方案
装载程序所需要的 DLL 库文件
这些 DLL库文件和一份清单文件可以在以下目录找到: "C:\Python26\Lib\site-packages\pythonwin\"
你将会需要以下几个文件:
- mfc90.dll
- mfc90u.dll
- mfcm90.dll
- mfcm90u.dll
- Microsoft.VC90.MFC.manifest
为了能在编译时复制这些文件,在你的 setup.py 文件中添加或者修改 data_files 选项:
mfcfiles = [os.path.join(mfcdir, i) for i in ["mfc90.dll", "mfc90u.dll", "mfcm90.dll", "mfcm90u.dll", "Microsoft.VC90.MFC.manifest"]]
data_files = [("Microsoft.VC90.MFC", mfcfiles),
]
setup(
data_files = data_files,
...
)
在这种条件下,运行 py2exe 将会把这些文件放入你的 dist 目录:
dist | +-Microsoft.VC90.MFC | | | +-Microsoft.VC90.MFC.manifest | +-mfc90.dll | +-... |-...
安装"vcredist"
另外一个解决方案是安装 "Microsoft Visual C++ 2008 Redistributable Package". 参照 Tutorial#Step522