2

I am using PyInstaller to create a single file exe for my python application. I am also using Pystray to create a tray icon with a menu on it.

When running the script directly it works, but when I use pyinstaller I get an error that the "icon.png" cannot be found. The file is in the root of my project directory.

I have tried --add-data "icon.png;icon.png" and --add-data "icon.png;." neither of those helped.

I also placed a copy of the icon.png file in pretty much every sub directory there was just to see if it would try to reference it from somewhere else.

Any ideas how to include the png file

Python Script:

from pystray import MenuItem as item
import pystray
from PIL import Image

def openConfig():
    webbrowser.open('http://localhost:5000', new=2)
def openAbout():
    webbrowser.open('http://localhost:5000/about', new=2)
def closeApp():
    os._exit(0)

image = Image.open("icon.png")
menu = (item('Configuration', openConfig), item('About', openAbout), item('Quit', closeApp))
icon = pystray.Icon("name", image, "MyApp Name", menu)

icon.run()

Command:

pyinstaller -w -F MyApp.py

Or:

pyinstall -w -F --add-data "icon.png;icon.png" MyApp.py

The Error: enter image description here

Developer Gee
  • 362
  • 3
  • 12
  • It is definitely an issue of not being able to reference the image. If I full the full path to the image it will work properly, but once I distribute the file that image wouldn't be in that location. After looking at other SO questions I have tried some other things, that also haven't worked. They all added a function to use the relative file path (which worked in the flask portion of my code, but not for the pstray portion). https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-in-the-exe-file – Developer Gee Feb 16 '22 at 00:18

1 Answers1

1

I finally figured it out. I put my image in a folder called static, and in the python I changed the code to the following:

icon_folder = os.path.join(sys._MEIPASS, 'static')
image = Image.open(icon_folder+"\icon.png")

Then in the command I ran:

pyinstaller --onefile --add-data "static;static" --add-data myApp.py
Developer Gee
  • 362
  • 3
  • 12