Python 如何在cxfreeze中适当地包含外部资源?

Python 如何在cxfreeze中适当地包含外部资源?,python,python-3.x,cx-freeze,Python,Python 3.x,Cx Freeze,我正在尝试使用将Python脚本构建到.exe文件中。但是,我的脚本使用一些外部数据文件,这些文件没有打包到创建的libary.zip文件中 例如,我的脚本位于src/中,外部数据位于src/data/中。我在build_exe_options中指定了include_files属性,但这只会将目录和文件复制到build目录中;它不会将它们添加到library.zip,脚本最终会在那里查找文件 即使我进入创建的library.zip并手动添加数据目录,我也会收到相同的错误。你知道如何让cxfree

我正在尝试使用将Python脚本构建到
.exe
文件中。但是,我的脚本使用一些外部数据文件,这些文件没有打包到创建的
libary.zip
文件中

例如,我的脚本位于
src/
中,外部数据位于
src/data/
中。我在
build_exe_options
中指定了
include_files
属性,但这只会将目录和文件复制到build目录中;它不会将它们添加到
library.zip
,脚本最终会在那里查找文件

即使我进入创建的
library.zip
并手动添加
数据
目录,我也会收到相同的错误。你知道如何让
cxfreeze
对这些外部资源进行适当的打包吗

setup.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(尝试读取资源文件的位置)

。。。打电话给

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'
回溯(最近一次呼叫最后一次):
文件“C:\Python33\lib\site packages\cx\u Freeze\initscripts\Console3.py”,第2行
7,在exec中(代码,m.uuu dict_uuu)
文件“my_script.py”,第94行,在
文件“my_script.py”,第68行,在运行中
文件“C:\workspaces\py\test\u script\src\tables.py”,第12行,在load\u数据中
raw\u gems=读取文件(“data/table\u 1.txt”)
文件“C:\workspaces\py\test\u script\src\fileutil.py”,第8行,在read\u文件中
打开(文件名为“r”)作为文件:
FileNotFoundError:[Errno 2]没有这样的文件或目录:
'C:\\workspaces\\py\\test\u script\\src\\build\\exe.win32-3.3\\library.zip\\data/table\u 1.txt'

以下结构适合我:

|-main.py
|-src
 |-utils.py (containing get_base_dir())
|-data
然后,在src目录中通过以下函数接收main.py位置相关的数据:

import os, sys, inspect
def get_base_dir():
   if getattr(sys,"frozen",False):
       # If this is running in the context of a frozen (executable) file, 
       # we return the path of the main application executable
       return os.path.dirname(os.path.abspath(sys.executable))
   else:
       # If we are running in script or debug mode, we need 
       # to inspect the currently executing frame. This enable us to always
       # derive the directory of main.py no matter from where this function
       # is being called
       thisdir = os.path.dirname(inspect.getfile(inspect.currentframe()))
       return os.path.abspath(os.path.join(thisdir, os.pardir))

如果您根据cx_Freeze文档包含数据,它将与
.exe
文件位于同一目录中(即不在zipfile中),该文件将用于此解决方案。

以下是文档的相关位:
import os, sys, inspect
def get_base_dir():
   if getattr(sys,"frozen",False):
       # If this is running in the context of a frozen (executable) file, 
       # we return the path of the main application executable
       return os.path.dirname(os.path.abspath(sys.executable))
   else:
       # If we are running in script or debug mode, we need 
       # to inspect the currently executing frame. This enable us to always
       # derive the directory of main.py no matter from where this function
       # is being called
       thisdir = os.path.dirname(inspect.getfile(inspect.currentframe()))
       return os.path.abspath(os.path.join(thisdir, os.pardir))