[英]Why am I getting Pyinstaller FileNotFoundError: [Errno 2] No such file or directory:
I am trying to create a standalone app from my Python script login.py although I am getting the error when running it:我正在尝试从我的 Python 脚本login.py创建一个独立的应用程序,尽管运行它时出现错误:
I have changed the login.spec and added the datas list as at the official documentation Using Spec Files我已经更改了login.spec并添加了数据列表,如官方文档Using Spec Files
/Users.../
is just the summarised path to make it readable here, it is complete in my file. /Users.../
只是使其在此处可读的汇总路径,它在我的文件中是完整的。
datas=[ ('/Users.../tiktok/*.png', '.' ), ('/Users/.../tiktok/*.jpg', '.' ) ],
Command to create the bundle:创建捆绑包的命令:
pyinstaller -D -F -w login.spec login.py
Traceback (most recent call last):
File "login.py", line 126, in <module>
File "login.py", line 82, in main
File "PIL/Image.py", line 2904, in open
FileNotFoundError: [Errno 2] No such file or directory: '/liberty.jpg'
[53518] Failed to execute script login
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
That means that the .jpg
and .png
are not bundled.这意味着.jpg
和.png
没有捆绑在一起。
login.py登录.py
from tkinter import *
from PIL import Image, ImageTk
import requests
import json
def main():
global rootLogin
rootLogin = Tk()
rootLogin.geometry("720x540")
rootLogin.eval('tk::PlaceWindow . center')
rootLogin.title("Login | bla bla bla")
# Logo
load = Image.open(curPath + "/liberty.jpg")
load = load.resize((136, 84), Image.ANTIALIAS)
render = ImageTk.PhotoImage(load)
img = Label(rootLogin, image=render)
img.image = render
img.place(x=293, y=86)
# Username
...
# Password
...
# Forgot password
...
# Login
...
rootLogin.mainloop()
if __name__ == '__main__':
import os
curPath = os.path.dirname(__file__)
print(curPath)
main()
login.spec登录规范
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['login.py'],
pathex=['/Users/.../tiktok'],
binaries=[],
datas=[ ('/Users.../tiktok/*.png', '.' ) , ('/Users/.../tiktok/*.jpg', '.' ) ],
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='login',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False )
app = BUNDLE(exe,
name='login.app',
icon=None,
bundle_identifier=None)
[DUPLICATED] [重复]
I found the answer here Bundling data files with PyInstaller (--onefile)我在这里找到了答案Bundling data files with PyInstaller (--onefile)
The problem is: pyinstaller unpacks data into a temporary folder问题是: pyinstaller 将数据解压到一个临时文件夹中
Just include the function:只需包括 function:
def resourcePath(relativePath):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
basePath = sys._MEIPASS
except Exception:
basePath = os.path.abspath(".")
return os.path.join(basePath, relativePath)
and reference it in: load = Image.open(curPath + "/liberty.jpg")
并在其中引用它: load = Image.open(curPath + "/liberty.jpg")
changed to: load = Image.open(resourcePath("liberty.jpg"))
改为: load = Image.open(resourcePath("liberty.jpg"))
# Logo
load = Image.open(resourcePath("liberty.jpg"))
load = load.resize((136, 84), Image.ANTIALIAS)
render = ImageTk.PhotoImage(load)
img = Label(rootLogin, image=render)
img.image = render
img.place(x=293, y=86)
Output # in development Output # 开发中
>>> resourcePath("liberty.jpg")
"/Users/marcelo/.../tiktok/liberty.jpg"
Output # # in production Output ##生产中
>>> resourcePath("liberty.jpg")
"/var/folders/yg/z47pdjvx3jg1y2f_58xmpd3m0000gn/T/_MEI4qD8I6/liberty.jpg"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.