Pyinstaller:unable 创建一个 .exe 文件,其中包含 .png 文件
Pyinstaller:unable to create onefile .exe which has .png file
我正在创建一个 tkinter 程序,我需要将其制作成 exe,以便将其发送给我的朋友。
所以对于这个程序,我需要添加一个 .png 和 .ico 文件。
这是我在 pyinstaller 中输入的代码来安装 exe
pyinstaller .\u.py -F --noconsole --add-data 'C:\Users\Binoy\Desktop\Techwiz\h.png; .' -i ".\h.ico"
这是我的 .spec 文件:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['U.py'],
pathex=['C:\Users\Binoy\Desktop\New folder'],
binaries=[],
datas=[('C:\Users\Binoy\Desktop\Techwiz\h.png', '.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='U',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False , icon='h.ico')
我还有一个 .txt 文件 regaridng 缺少模块但是在 .txt 中,他们说这些模块不会影响 .exe 所以我不关心 it.At 首先是 运行 没有任何问题,但是如果我删除 .png 或者我的一个朋友下载它,它会显示错误 Failed to execute script U
。
所以我尝试了 resource_path()
函数 (),但没有用。
所以我创建了一个 .bat 文件 (),并得到了这个错误:
Traceback (most recent call last):
File "U.py", line 19, in <module>
File "tkinter\__init__.py", line 4062, in __init__
File "tkinter\__init__.py", line 4007, in __init__
_tkinter.TclError: couldn't open "C:/Users/Binoy/Desktop/Techwiz/h.png": no such file or directory
[8736] Failed to execute script U
有什么方法可以将图像 (.png) 与 .exe 文件一起存储为单个 .exe 文件吗?
Joran Beasley 的评论是正确的。您的路径设置可能存在一些问题。如果我想编译一个包含外部文件的可执行文件,我使用以下方法
from PIL import Image
import os
import sys
#check if its a compiled exe
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
filePath = os.path.join(sys._MEIPASS, "test_picture.png")
#otherwise it must be a script
else:
scriptPath = os.path.realpath(os.path.dirname(sys.argv[0]))
filePath = os.path.join(scriptPath, "test_picture.png")
f = Image.open(filePath).show()
在这个例子中,'test_picture' 将相对于脚本,如果 运行 它作为一个 .py 文件。否则,它将存储在临时 _MEI 文件夹中。后一个是通过
定义的
datas = [...]
您已经在规范文件中完成了。
此外,如果您 运行 一个 pyinstaller 可执行文件,您可能需要检查以下路径。
C:\用户...\AppData\Local\Temp\
应该有一个名为_MEI 的文件夹,后跟一些数字。这是将提取您的可执行文件以及您包含的所有数据的文件夹
另见
https://pyinstaller.readthedocs.io/en/stable/runtime-information.html
What is sys._MEIPASS in Python
我正在创建一个 tkinter 程序,我需要将其制作成 exe,以便将其发送给我的朋友。 所以对于这个程序,我需要添加一个 .png 和 .ico 文件。 这是我在 pyinstaller 中输入的代码来安装 exe
pyinstaller .\u.py -F --noconsole --add-data 'C:\Users\Binoy\Desktop\Techwiz\h.png; .' -i ".\h.ico"
这是我的 .spec 文件:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['U.py'],
pathex=['C:\Users\Binoy\Desktop\New folder'],
binaries=[],
datas=[('C:\Users\Binoy\Desktop\Techwiz\h.png', '.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='U',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False , icon='h.ico')
我还有一个 .txt 文件 regaridng 缺少模块但是在 .txt 中,他们说这些模块不会影响 .exe 所以我不关心 it.At 首先是 运行 没有任何问题,但是如果我删除 .png 或者我的一个朋友下载它,它会显示错误 Failed to execute script U
。
所以我尝试了 resource_path()
函数 (
Traceback (most recent call last):
File "U.py", line 19, in <module>
File "tkinter\__init__.py", line 4062, in __init__
File "tkinter\__init__.py", line 4007, in __init__
_tkinter.TclError: couldn't open "C:/Users/Binoy/Desktop/Techwiz/h.png": no such file or directory
[8736] Failed to execute script U
有什么方法可以将图像 (.png) 与 .exe 文件一起存储为单个 .exe 文件吗?
Joran Beasley 的评论是正确的。您的路径设置可能存在一些问题。如果我想编译一个包含外部文件的可执行文件,我使用以下方法
from PIL import Image
import os
import sys
#check if its a compiled exe
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
filePath = os.path.join(sys._MEIPASS, "test_picture.png")
#otherwise it must be a script
else:
scriptPath = os.path.realpath(os.path.dirname(sys.argv[0]))
filePath = os.path.join(scriptPath, "test_picture.png")
f = Image.open(filePath).show()
在这个例子中,'test_picture' 将相对于脚本,如果 运行 它作为一个 .py 文件。否则,它将存储在临时 _MEI 文件夹中。后一个是通过
定义的datas = [...]
您已经在规范文件中完成了。
此外,如果您 运行 一个 pyinstaller 可执行文件,您可能需要检查以下路径。
C:\用户...\AppData\Local\Temp\
应该有一个名为_MEI 的文件夹,后跟一些数字。这是将提取您的可执行文件以及您包含的所有数据的文件夹
另见
https://pyinstaller.readthedocs.io/en/stable/runtime-information.html
What is sys._MEIPASS in Python