我正在尝试使用cxfreeze将我的 Python 脚本构建到一个.exe
文件中。但是,我的脚本使用了一些未打包到libary.zip
创建的文件中的外部数据文件。
例如,我的脚本位于src/
,外部数据位于src/data/
. 我在 中指定了include_files
属性build_exe_options
,但这只会将目录和文件复制到构建目录中;它不会将它们添加到library.zip
,这是脚本最终查找文件的地方。
即使我进入创建library.zip
并手动添加data
目录,我也会收到相同的错误。知道如何cxfreeze
适当地打包这些外部资源吗?
安装程序.py
from cx_Freeze import setup, Executable
build_exe_options = {"includes" : ["re"], "include_files" : ["data/table_1.txt", "data/table_2.txt"]}
setup(name = "My Script",
version = "0.8",
description = "My Script",
options = { "build_exe" : build_exe_options },
executables = [Executable("my_script.py")])
fileutil.py(它尝试读取资源文件的地方)
def read_file(filename):
path, fl = os.path.split(os.path.realpath(__file__))
filename = os.path.join(path, filename)
with open(filename, "r") as file:
lines = [line.strip() for line in file]
return [line for line in lines if len(line) == 0 or line[0] != "#"]
...打电话给...
read_file("data/table_1.txt")
错误回溯
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 2
7, in <module> exec(code, m.__dict__)
File "my_script.py", line 94, in <module>
File "my_script.py", line 68, in run
File "C:\workspaces\py\test_script\src\tables.py", line 12, in load_data
raw_gems = read_file("data/table_1.txt")
File "C:\workspaces\py\test_script\src\fileutil.py", line 8, in read_file
with open(filename, "r") as file:
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\workspaces\\py\\test_script\\src\\build\\exe.win32-3.3\\library.zip\\data/table_1.txt'