我使用 pythons cx_freeze 模块为 Windows 创建了一个应用程序。该应用程序运行 openpyxl 模块,该模块对脚本运行良好,但在冻结时无法找到 .constants.json 文件。显示以下错误。FileNotFoundError:[Errno 2] 没有这样的文件或目录:'C:....\exe.win-amd64-3.4\library.zip\openpyxl.constants.json'
我找到了一个解决方法(https://cx-freeze.readthedocs.org/en/latest/faq.html#using-data-files),详情如下:
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
我的问题是我在哪里粘贴这段代码?它在 setup.py 文件中吗?或者别的地方?