gpt4 book ai didi

python - 在 PyInstaller 中将数据与 .spec 文件捆绑

转载 作者:太空宇宙 更新时间:2023-11-03 17:35:48 24 4
gpt4 key购买 nike

所以我已经阅读了这里的所有问题,但我无法理解为什么这不起作用。我有一个如下所示的 .spec 文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['newtestsphinx.py'],
pathex=['C:\\Program Files (x86)\\speechfolder'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None,
excludes=None,
cipher=block_cipher)
pyz = PYZ(a.pure,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas + [('grammar2.jsgf', 'C:\\Program Files (x86)\\speechfolder\\grammar2.jsgf', 'DATA')],
name='newtestsphinx.exe',
debug=False,
strip=None,
upx=True,
console=True )

就像所有示例一样,如果我理解它们,我将 'grammar2.jsgf' 添加到根目录中的包中,我相信其格式是 ['path_to_put_in', 'path_its_in_now', 'label']

然后我运行命令来创建新文件:

pyinstaller --onefile newtestsphinx.spec

我现在在代码中做的第一件事是:

print os.path.isfile('grammar2.jsgf')

它返回 false 100%,并且我的程序也找不到使用它的文件。任何帮助都会很棒,谢谢!

最佳答案

当前的问题是 pyinstaller 在运行时应将一堆必要的支持文件提取到临时目录中。当尝试访问这些支持文件时,您需要预先使用正确的目录名来访问这些文件。来自 docs :

import sys
import os

if getattr(sys, 'frozen', False):
# we are running in a |PyInstaller| bundle
basedir = sys._MEIPASS
else:
# we are running in a normal Python environment
basedir = os.path.dirname(__file__)

那么当尝试访问您的文件时:

print os.path.isfile(os.path.join(basedir, 'grammar2.jsgf'))

您应该看到它返回 True。另一件有用的事情是打印出 basedir,并确保执行不会结束,使用一些简单的东西,例如:

raw_input('Press enter to end execution.')

这将允许您查看临时目录的位置 - 然后您可以探索一下并了解它是如何工作的。

关于python - 在 PyInstaller 中将数据与 .spec 文件捆绑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com