0

I am trying to create an executable of my script, but running the .exe does not find the image. I have tried both onefile and multiples and pasting the images inside but it does not work.

These would be my images.

root = Tk()
root.title("Tai Project")
root.geometry("600x600")
root.resizable(0, 0)
img = PhotoImage(file="Tai_Project\ccc.png")
img_opo = PhotoImage(file="Tai_Project\opo.png")
img_label = PhotoImage(file="Tai_Project\labeltest.png")
1
  • If you're using PyInstaller to make your .exe, see this Commented May 12, 2022 at 10:30

2 Answers 2

0

You can use this function for all paths:

import sys
import os


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)

Usage example:

img = PhotoImage(file=resource_path("Tai_Project\ccc.png"))

Also in your .spec file you need to match directories in "datas" section like this:

a.datas += [("Tai_Project\ccc.png","C:\\Users\\username\\projects\\my_project\\Tai_Project\\ccc.png", "DATA")]

This way your files will be included in .exe and will be available in TEMP directory which is created when you run your program.

Sign up to request clarification or add additional context in comments.

20 Comments

I have tried to copy my route, in the .spec: a.datas += [("Tai_Project\ccc.png","C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project\\ccc.png", "DATA")] But it keeps giving me this error "C:\Users\Usuario\AppData\Local\Temp_MEI199162\Tai_Project\ccc.png": no such file or directory
So have you replaced all paths to resource_path(path) calls?
So first thing is to include your files in exe file. It is done by this spec file. When you run your program (exe file) it creates some folder structure that is defined in spec file under user's TEMP directory. So program should look for files in some TEMP directory instead of your usual paths. Funtion that I shared allows you to convert this paths.
I edit the post with the modifications and the same error, I am putting something wrong
First thing - replace single backslashes to double backslashes as you need to escape backslash symbol. Second thing - can you pls check contents of temp directory which is created when you run your program? Do you have Tai_Project dir there?
|
0

Actually .spec

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['Tai_Interface.py'],
    pathex=[],
    binaries=[],
    datas=[("Tai_Project\\ccc.png","C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project\\ccc.png", "DATA"), ("Tai_Project\\ccc.png","C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project\\odo.png", "DATA"), ("Tai_Project\\ccc.png","C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project\\labeltest.png", "DATA")],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    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='Tai_Interface',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
Traceback (most recent call last):
  File "Tai_Interface.py", line 28, in <module>
  File "tkinter\__init__.py", line 4064, in __init__
  File "tkinter\__init__.py", line 4009, in __init__
_tkinter.TclError: couldn't open "C:\Users\Usuario\Desktop\Python1\Tai_Project\dist\Tai_Interface.exe\Tai_Project\ccc.png": no such file or directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.