Reputation: 1083
I am trying to create a standalone app from my Python script login.py although I am getting the error when running it:
I have changed the login.spec and added the datas list as at the official documentation Using Spec Files
/Users.../
is just the summarised path to make it readable here, it is complete in my file.
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.
login.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)
Upvotes: 0
Views: 4855
Reputation: 1083
[DUPLICATED]
I found the answer here Bundling data files with PyInstaller (--onefile)
The problem is: pyinstaller unpacks data into a temporary folder
Just include the 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")
changed to: 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
>>> resourcePath("liberty.jpg")
"/Users/marcelo/.../tiktok/liberty.jpg"
Output # # in production
>>> resourcePath("liberty.jpg")
"/var/folders/yg/z47pdjvx3jg1y2f_58xmpd3m0000gn/T/_MEI4qD8I6/liberty.jpg"
Upvotes: 3