问题描述
我也是第一次尝试构建包含数据文件的简单--onefile exe,但是Pyinstaller在构建.exe时似乎找不到它们. --onedir构建似乎可以正常工作.
I too am trying for the first time to build a simple --onefile exe which includes data files, but Pyinstaller doesn't seem to find them when building the .exe. A --onedir build seems to work fine.
在这一点上,我也正在使用--debug开关.我能够运行onefile可执行文件,并且可以看到它似乎开始工作.程序找到了(sys._MEIPASS)临时目录ok(按照指示打印所需的目录名),但是当它从temp目录中查找第一个数据文件时,报告没有这样的文件或目录"错误.我在.exe上使用了archiveviewer.py,然后在其中找不到所需的数据文件-这似乎是问题所在,但我不知道为什么.用于构建的数据文件位于规范文件描述的目录中.我完整的规格文件是
I'm using the --debug switch at this point as well. I am able to run the onefile executable and can see that it appears to start working. The program finds the (sys._MEIPASS) temp directory ok (prints the needed directory name as directed) but reports a "no such file or directory" error when it looks for the first data file from the temp directory. I used archiveviewer.py on the .exe and DIDN'T find the needed data files there-- which seems to be the problem, but I can't figure out why. Data files for the build are in the directory the spec file describes. My complete spec file is
# -*- mode: python -*-
a = Analysis(['develop6.py'],
pathex=['C:\\PYINST20'],
hiddenimports=[],
hookspath=None)
a.datas += [ ('conlist.txt', 'C:\\pyinst20\\conlist.txt', 'DATA'), ('imageblank.gif', 'C:\\pyinst20\\imageblank.gif', 'DATA')]
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name=os.path.join('dist', 'develop6.exe'),
debug=True,
strip=None,
upx=True,
console=True )
推荐答案
(这个问题很旧,但是这是我发现解决同一问题的唯一来源之一,我将在这里分享我的解决方案,以防万一.某人)
(this question is old but a it is one of the only sources I found to solve the same problem, I will share my solution here in case it could help someone)
要以--onefile模式将数据文件添加到脚本中,需要做两件事.
There is two main things to do to add data files to your script in --onefile mode.
在脚本中,调整路径以查找捆绑软件中的数据文件.根据PyInstaller文档此处,该可执行文件是从一个临时文件启动的,因此您的路径必须照顾这个动态部分:
In your script, adapt your paths to find the datafiles in the bundle.According to PyInstaller documentation here,the executable is launched from a temporary file, so your path must take care of this dynamic part :
对于具有以下相对路径的文件:./your/file/is/here.ext
For a file with the following relative path :./your/file/is/here.ext
代码为:
import sys
wd = sys._MEIPASS
file_path = os.path.join(wd,<your>,<file>,<is>,<here>)
注意:要使您的代码也可以在其他上下文中使用,您可以执行以下操作:
Note : to make your code also work on other contexts, you can do the following :
import sys
import os
try:
wd = sys._MEIPASS
except AttributeError:
wd = os.getcwd()
file_path = os.path.join(wd,<your>,<file>,<is>,<here>)
2.在PyInstaller规范中添加数据文件路径
根据PyInstaller文档此处 ,有两种方法可以将数据文件添加到捆绑软件中:
2. Add the data files paths in the PyInstaller specs
According to PyInstaller documentation here, there is two ways to add data files to the bundle :
-
从脚本目录运行
pyinstaller <yourscript.py>
时,将选项--add-files
和文件作为参数传递
Pass the option
--add-files
and the files as parameters when runningpyinstaller <yourscript.py>
from your script directory
首先通过导航到脚本目录并运行pyi-makespec <yourscript.py>
来生成规格文件,然后将文件添加到元组data=[]
的列表中.元组包含文件的实际路径以及其中的路径您的捆绑包.如果您遵循了第一点,这应该看起来像datas = [('/your/file/is/here.ext','/your/file/is/')]
First generate a spec file by navigating to your script directory and running pyi-makespec <yourscript.py>
, then add your files to the list of tuples data=[]
.The tuples contains the actual path to the file and the path withinyour bundle. If you followed the first point, this should look likedatas = [('/your/file/is/here.ext','/your/file/is/')]
然后根据您的规格文件运行PyInstall <yourscript.spec>
来构建捆绑包.
Then run PyInstall <yourscript.spec>
to build the bundle, based on your specs file.
这篇关于pyinstaller onefile找不到数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!