Pyinstaller to create windows .exe does not yield specified icon.

1,407 views
Skip to first unread message

Paul Fishback

unread,
Mar 8, 2022, 4:00:51 AM3/8/22
to PyInstaller
I see this issue discussed elsewhere, but various suggestions haven't helped me. 

I have a working script that I've successfully converted to a windows .exe.

Because I'll eventually need to add many hidden imports and data files, I'm using a spec file, which is as follows. However, for simplicity, I'll provide a stripped down version with nothing added:

block_cipher = None
a = Analysis(['my_script.py'],
             pathex=[],
             binaries=[],
             datas=[],
             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='my_script',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )

app = BUNDLE(name='my_script.exe',icon=
C:\Users\MyName\Desktop\my_icon.ico',bundle_identifier=None,
info_plist={
        'LSEnvironment': {
            'LANG': 'de_DE.UTF-8',
            'LC_CTYPE': 'de_DE.UTF-8'
        }})

For some reason, I'm only getting the pyinstaller default icon. 

Suggestions?

Edward LOWE [11Y1]

unread,
Mar 8, 2022, 5:58:09 AM3/8/22
to PyInstaller
try adding icon to the EXE

Edward LOWE [11Y1]

unread,
Mar 8, 2022, 5:58:15 AM3/8/22
to PyInstaller

Paul Fishback

unread,
Mar 8, 2022, 11:48:25 AM3/8/22
to PyInstaller
Edward LOWE ,

Thanks for the responses and suggestions.

I'm still having difficulties. Here's what I tried:

1. I included the icon location to the EXE as you suggested by adding the argument
icon='\\Users\\MyName\\Desktop\\my_icon.ico',

2. Based upon the Stack Overflow post you passed along, I included the icon in my data via
datas=[('\\Users\\MyName\\Desktop\\eeg_2022_win.ico', '.')]

3. I deleted the icon reference in BUNDLE.

This led to the error 

Unable to find "\\Users\\MyName\\Desktop\\eeg_2022_win.ico" when adding binary and data files.

My .spec file and .ico are located in the same directory. It's not clear to me whether I should be using absolute or relative file paths, so I changed
datas above to 
datas=[('eeg_2022_win.ico', '.')]

This led to a new error message 

Unable to open icon file \Users\MyName\Desktop\eeg_2022_win.ico

I also tried including the icon option in BUNDLE, using both absolute and relative file paths. This led again to the second error message immediately above 

I haven't worked with .ico file types before. To create my icon, I simply went to an online png-to-ico convertor. Are there particular criteria the ico must satisfy in order for pyinstaller to open it and associate it with my .exe ?

Thanks. 

Paul Fishback

unread,
Mar 8, 2022, 12:25:05 PM3/8/22
to PyInstaller
For clarification, the name of the icon though out my sample code snippet outlining my problem should be given as "my_icon.ico" 
The name "eeg_2022_win" is for my actual larger application. Apologies for the confusion. 

Paul Fishback

unread,
Mar 9, 2022, 3:53:12 AM3/9/22
to PyInstaller
The problem is not with pyinstaller but is related to the "icon cache" so to speak. I closed windows file explorer, opened it up again, and the icon appeared. 

To summarize:
1. Include icon in data using relative path name.
2. Removed icon from BUNDLE
3. Inserted icon='my_icon.ico' into EXE. 
4. ran pyinstaller myspec.spec
5. dist folder shows no icon; close file explorer, reopen, and all is fine. 

Verified this twice. 

Eric Fahlgren

unread,
Mar 9, 2022, 3:53:18 AM3/9/22
to pyins...@googlegroups.com
As Edward says, should be in EXE not the BUNDLE/COLLECT of your .spec.  I've always used absolute paths:

    icon = os.path.join(BUILD_ROOT, 'gui/resources/app.ico'),

where BUILD_ROOT is something like "c:/my/build".

When I run Image Magick (magick identify ...) on the icon file, I get the following.  I think Windows prefers icons sized at 16 pixel intervals, but I think it works with any old thing of reasonable size as long as you can tolerate the interpolation artifacts.

gui/resources/app.ico[0] PNG 256x256 256x256+0+0 8-bit sRGB 51449B 0.000u 0:00.001
gui/resources/app.ico[1] ICO 48x48 48x48+0+0 8-bit sRGB 0.000u 0:00.001
gui/resources/app.ico[2] ICO 32x32 32x32+0+0 8-bit sRGB 0.000u 0:00.000
gui/resources/app.ico[3] ICO 16x16 16x16+0+0 8-bit sRGB 66551B 0.000u 0:00.000


--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/3ef0850b-7617-4e39-bc0c-0b0b4e8d917dn%40googlegroups.com.

Nico Zanferrari

unread,
Mar 9, 2022, 3:53:27 AM3/9/22
to PyInstaller
Hi Paul,

you can look at my spec for a working example, and also try with the provided icon which surely works.

The icon parameter I've used inside EXE is relative to the bulding folder:

icon='extras\\icons\\py4web.ico',

and you don't need it on the datas parameter.

Nico

--

Edward LOWE [11Y1]

unread,
Mar 9, 2022, 3:53:32 AM3/9/22
to PyInstaller
I think absolute paths should include the drive letter as well e.g. "C:\\Users...."

I'm not sure why it can't open the file. One thing that comes to mind is that data files will be put in the second part of the tuple "." in your case. So if your code is trying to open it from somewhere else instead of the current folder it might not be able to open it. This is purely guesswork though as I haven't used data files myself.

Reply all
Reply to author
Forward
0 new messages