0

I've converted my python script with tkinter module to a standalone executable file with PyInstaller but it doesn't work without image.png file in the same patch. How I can add this .png file to my app. And why .exe file have an enormous weight of ~350 Mb?

  • Sounds like you might be using pyinstaller, but if you are not it would be helpful to know what you are using to create the exe. When using pyinstaller there can be some issues with paths. In that case, [this](https://stackoverflow.com/questions/41870727/pyinstaller-adding-data-files) answer may be useful to you. – nikost Jan 19 '22 at 15:14
  • Yeah, right, I've already edited, but I'm using tkinter for GUI. ((auto-py-to-exe) - add data but it doesn't work) – Aleksandr Tyshkevich Jan 20 '22 at 06:38
  • You better study [PyInstaller Run-time Information](https://pyinstaller.readthedocs.io/en/stable/runtime-information.html). – acw1668 Jan 20 '22 at 09:22

2 Answers2

1

I had the exact same situation with Tkinter and a single image needed in the GUI.

I combined Aleksandr Tyshkevich answer here and Jonathon Reinhart's answer here Pyinstaller adding data files as I need to send just the exe file to others, so it needs to work from any location.

I have one python file named gui.py. In gui.py I included the code:

import sys
import os
from PIL import Image

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

path = resource_path("logo.png")


# When opening image
logo = Image.open(path)

In the terminal I used:

pyinstaller --onefile --add-data "logo.png;." gui.py

I ran into a problem when I tried to use a colon in the line above, as I'm using Windows OS so needed to use a semi-colon.

Catherine
  • 11
  • 2
0

It works:

import os

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)

path = resource_path("image.png")
photo = tk.PhotoImage(file=path)
Vincent Doba
  • 4,343
  • 3
  • 22
  • 42