我已经使用Pyinstaller创建了一个exe文件。
pyinstaller.exe --onefile --icon='Loco.ico program.py
在程序中,我在绘图中包含了一个图像,当我在其文件夹中单独运行该程序时,会得到以下信息:
IOError: [Errno 2] No such file or directory: 'Logo.png'
一种解决方案是将图像包含在exe的文件夹中,如下面的链接所示:
pyinstaller不显示图像和图标
但是再说一遍–onefile的全部目的就是拥有它,而不需要额外的映像。我认为解决方案可能在此链接中,但我还不了解。
使用PyInstaller捆绑数据文件(–onefile)
我的规格文件如下所示:
# -*- mode: python -*- a = Analysis(['AMOS_Visualizer.py'], pathex=['C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting'], hiddenimports=[], hookspath=None, runtime_hooks=None) pyz = PYZ(a.pure) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='AMOS_Visualizer.exe', debug=False, strip=None, upx=True, console=True , icon='AMOS.ico')
编辑:
我相信我找到了解决问题的办法。
# -*- mode: python -*- a = Analysis(['AMOS_Visualizer.py'], pathex=['C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting'], hiddenimports=[], hookspath=None, runtime_hooks=None) for d in a.datas: if 'pyconfig' in d[0]: a.datas.remove(d) break a.datas += [('Logo.png','C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting\\Logo.png', 'Data')] pyz = PYZ(a.pure) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='AMOS_Visualizer.exe', debug=False, strip=None, upx=True, console=True, icon='C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting\\AMOS.ico')
并将以下内容添加到我的main.py脚本中
def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) Logo = resource_path("Logo.png")