Search code examples
pythonpyinstallerexe

Exe file wont launch after converting .py to .exe with pyinstaller


I have a project for minecraft, and after converting it to exe with this command:

pyinstaller "F:\pythonprojetcs\minecraft file mover\splashscreen.py" -F --icon="F:\pythonprojetcs\minecraft file mover\app.ico"

It wont launch. This is the error:

Traceback (most recent call last):
  File "splashscreen.py", line 21, in <module>
    image = tk.PhotoImage(file=image_file)
  File "tkinter\__init__.py", line 4064, in __init__
  File "tkinter\__init__.py", line 4009, in __init__
_tkinter.TclError: couldn't open "C:\Users\REINER~1\AppData\Local\Temp\_MEI248722\data\image.png": no such file or directory
[4692] Failed to execute script splashscreen

It works fine when its in the .py format, with to errors. Its says that tkinter is the error, but I don't understand it.

The code:

# create a splash screen, 80% of display screen size, centered,
# displaying a GIF image with needed info, disappearing after 5 seconds
import os
import tkinter as tk
import shutil
import time


root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width*0.8, height*0.8, width*0.1, height*0.1))

image_file = os.path.dirname(__file__) + '\\data\\image.png'
image = tk.PhotoImage(file=image_file)
canvas = tk.Canvas(root, height=height*0.8, width=width*0.8, bg="brown")
canvas.create_image(width*0.8/2, height*0.8/2, image=image)
canvas.pack()

root.after(2000, root.destroy)
root.mainloop()

print("Világ vagy textúrát akkarsz? (világ = 1 / textúra = 2 / világ másolás = 3 / tutorial = 4)")
choosing = input()

if choosing == "1":
    print("\n")
    print("\n")
    print("Ok, szóval világ.")
    print("Hol van?")
    original = input()
    target = 'C:\\Users\\Refi\\AppData\\Roaming\\.minecraft\\saves'
    shutil.move(original,target)
    time.sleep(1)
    print("Kész!")
    print("Ha ezt írja ki: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Reiner Regő\\Downloads\\test', akkor az a fálj nem létezik!")
    print("Kérlek várj! Ne zárd be!")
    time.sleep(3)
if choosing == "2":
    print("\n")
    print("\n")
    print("Ok, szóval textúra.")
    print("Hol van?")
    original = input()
    target = 'C:\\Users\\Refi\\AppData\\Roaming\\.minecraft\\resourcepacks'
    shutil.move(original,target)
    time.sleep(1)
    print("Kész!")
    print("Ha ezt írja ki: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Reiner Regő\\Downloads\\test', akkor az a fálj nem létezik!")
    print("Kérlek várj! Ne zárd be!")
    time.sleep(3)

if choosing == "3":
    print("\n")
    print("\n")
    print("Ok, szóval világot akarsz másolni.")
    print("Mi a neve?")
    inputfromuser = input()
    original = 'C:\\Users\\Refi\\AppData\\Roaming\\.minecraft\\saves\\' + inputfromuser
    target = 'D:'
    shutil.move(original,target)
    time.sleep(1)
    print("Kész! A D:-ben fogod megtlálni!")
    print("Ha ezt írja ki: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Refi\\Downloads', akkor az a fálj nem létezik!")
    print("Kérlek várj! Ne zárd be!")
    time.sleep(3)

    print('\n')
    input("nyomd meg az entert a kilépéshez")

This copies files from one diretory from another, the splashscreen code is just an example. Please help!


Solution

  • Your script is trying to access image files from a directory relative to the script itself; this works when the script is installed unpacked, with resources actually on the file system. But when bundled into an executable, that can't work; the image isn't going to be there unless you've copied it in with the executable (an ugly hack of a solution); you need to bundle it into the executable itself in such a way that it can be extracted at runtime by your script.

    You need to read the docs on spec files, specifically, adding data files (to bundle the data with the executable when you build it) and using data files from a module (to extract the bundled data in memory at runtime).

    You'll only have access to the data from the file, it won't be a file on disk anymore, so you'll need to use alternate means of loading, e.g. from base64 encoded data.