Mihkel
Mihkel

Reputation: 709

PyInstaller EXE Changes Back To Original Icon

I have created a standalone .exe file with PyInstaller using this command: pyinstaller --onefile -i "icon0.ico" test.py -w. When I open the dist folder the exe gets put into it shows the icon I used but the moment I copy or move it from that folder the icon disappears and it returns back to the stock PyInstaller icon.

Oddly enough if I rename the file the icon stays like it is supposed to, but I can't use this as a solution since I have other files that depend on my exe being a specific filename. I used Resource Hacker to view the icon contents, I completely replaced the stock icon with my icon but after saving it nothing changed, still the same old stock PyInstaller icon. Yes, my .ico file had all the different 256x256, 128x128, 64x64, 48x48, 32x32 and 16x16 sizes.

What can I do to fix this?

PyInstaller version: 3.4

Python version: 3.7.2

Upvotes: 4

Views: 1815

Answers (4)

Tom
Tom

Reputation: 100

I usually use:

pyinstaller --onefile -w --icon=*icon name*.ico test.py

Upvotes: 2

Fighter178
Fighter178

Reputation: 413

Solution:

pyinstaller --noconfirm --onefile --name=filename --icon=icon.ico script.py

Works well, but the .ico file must be in the same directory as the .exe file. Any other options work as well, but haven't tried removing --onefile.

Note: --name required. I am not certain why.

Upvotes: 1

PyNZ
PyNZ

Reputation: 3

Assuming python 3.10, you need to make the icon known within the pyInstaller environment. My batch file is:

<full_path>\pyinstaller -wF --onefile --add-binary myicon.ico;. --icon myicon.ico app.py 2> build_log.txt

and in the python file app.py add:

import os, sys
if getattr(sys, 'frozen', False):
    # If the application is run as a bundle, the PyInstaller bootloader
    # extends the sys module by a flag frozen=True and sets the app 
    # path into variable _MEIPASS'.
    application_path = sys._MEIPASS
else:
    application_path = os.path.dirname(os.path.abspath(__file__))

then refer to the icon with:

os.path.join(application_path,'myicon.ico')

Upvotes: 0

Jaeho Song
Jaeho Song

Reputation: 73

I found a solution to this issue, it might apply to your case too. See herefor the related question.

I had same issue, I tried both putting pyinstaller ... --icon=icon/path/icon.ico ... main.py and editing pyinstaller.spec file,

exe = EXE(pyz,
          ...
          console=False , icon='C:\\icon\\path\\icon.ico')

But none of these solutions seems to work.
So, as mentioned on the link above, changing/renaming the directory /dist/ or renaming the .exe file immidiately changes the icon.

Upvotes: 0

Related Questions