本文譯自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