OK解決:
1)setup.py
import sys
from cx_Freeze import setup, Executable
EXE1 = Executable(
# what to build
script = "foo.py",
initScript = None,
base = 'Win32GUI',
targetDir = "dist",
targetName = "foo.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = True,
appendScriptToLibrary = False,
icon = 'foo.ico'
)
setup(
version = "9999",
description = "...",
author = "...",
name = "...",
options = {"build_exe": {"includes": includes,
"excludes": excludes,
"packages": packages,
"path": sys.path,
"append_script_to_exe":False,
"build_exe":"dist/bin",
"compressed":True,
"copy_dependent_files":True,
"create_shared_zip":True,
"include_in_shared_zip":True,
"optimize":2,
}
},
executables = [EXE1]
)
2)foo.py頭:
import os
import sys
if getattr(sys,'frozen',False):
# if trap for frozen script wrapping
sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin'))
sys.path.append(os.path.join(os.path.dirname(sys.executable),'bin\\library.zip'))
os.environ['TCL_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tcl')
os.environ['TK_LIBRARY'] = os.path.join(os.path.dirname(sys.executable),'bin\\tk')
os.environ['MATPLOTLIBDATA'] = os.path.join(os.path.dirname(sys.executable),'bin\\mpl-data')
通過'build_exe':「dist/data」我已經能夠將dll/pyd文件整理到一個子目錄中,但是我現在收到導入錯誤。將sys.path + ['./ data']添加爲路徑選項不能解決此問題。 – Naib
pyd/dll文件是編譯庫,不能從zip文件中加載(儘管可以在程序需要它們之前從壓縮文件中提取它們)。更改'sys.path'應該可以用於pyd文件,但是很難更改DLL加載的位置 - 請參閱[此MSDN文檔頁面](http://msdn.microsoft.com/zh-cn/library/windows/desktop /ms682586%28v=vs.85%29.aspx#search_order_for_desktop_applications)。 –