Home > Software design >  Convert tkinter script which uses multiple image resources to .exe | image no such file or directory
Convert tkinter script which uses multiple image resources to .exe | image no such file or directory

Time:05-17

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.

def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

root = Tk()
root.title("Tai Project")
root.geometry("600x600")
root.resizable(0, 0)

img = PhotoImage(file=resource_path("Tai_Project\ccc.png"))
img_opo = tkinter.PhotoImage(file=resource_path("Tai_Project\opo.png"))
img_label = tkinter.PhotoImage(file=resource_path("Tai_Project\labeltest.png"))

.spec

 a = Analysis(
    ['Tai_Interface.py'],
    pathex=['C:\\Users\\Usuario\\Desktop\\Python1\\Tai_Project'],
    binaries=[],
    datas=[('ccc.png','.'),('opo.png','.'),('labeltest.png','.')],
    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=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

And the error message:

_tkinter.TclError: couldn't open "C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png": no such file or directory

And my route is: C:\Users\Usuario\Desktop\Python1\Tai_Project

CodePudding user response:

_tkinter.TclError: couldn't open "C:\Users\Usuario\AppData\Local\Temp_MEI88482\Tai_Project\ccc.png": no such file or directory

This means that your spec file is written wrong and your files are not included in your temp dir.

pathex is path to your python.exe folder (not project)

datas must connect relative TEMP dir paths and absolute path to your files

For example:

I have file with absolute path:

C:\Users\user\PycharmProjects\project\file.json

In code I refer to it like this:

resource_path("file.json")

Spec file must have:

datas = [("file.json","C:\\Users\\user\\PycharmProjects\\project\\file.json", "DATA")]
  • Related