Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyInstaller icon option doesn't work on Mac

I ran the following command on my mac and created an .app file.

pyinstaller --icon icon.icns --noconsole -n testApp main.py

However, the generated .app file does not show the icon.

enter image description here

icon.icns is specified as an icon file in info.plist.

enter image description here

The Content/Resouces folder contains icon.icns.

enter image description here

When I run the .app file, I see an icon on the Dock.

However, the icon is not reflected in the .app file itself. Why is this?

like image 476
taichi Avatar asked Dec 31 '22 00:12

taichi


1 Answers

You should use the command for creating a macOS app bundle

If specified correctly, the icon.icns will be copied into the Resources folder and applied to the testApp.app bundle in the dist directory, after running the pyinstaller command. (don't try .ico as suggested, that's for Windows).

I just tried creating a simple Python app using PySide2, and it included the icon file in the app bundle with no problems.

Given that the icon.icns is in the same directory as main.py:

Your command should be:

$ pyinstaller --onefile --windowed --icon icon.icns --name testApp main.py

This will produce the app bundle, and you should be able to run it just fine.


But wait, there is even more...

Sadly, the story doesn't quite end here.

SPOILER:

To do things "the right way" and make macOS happy, you should also include the:

--osx-bundle-identifier 'YOUR_IDENTIFIER'

That way the spec file will include the info for the Info.plist file that gets produced.

Also there should be a section in the spec file describing the Info.plist contents, something in the lines of:

app = BUNDLE(exe,
         name='testApp.app',
         icon='icon.icns',
         bundle_identifier='com.youridentifier',
         info_plist={
            'NSPrincipalClass': 'NSApplication',
            'NSAppleScriptEnabled': False,
            'CFBundleDocumentTypes': [
                {
                    'CFBundleTypeName': 'My File Format',
                    'CFBundleTypeIconFile': 'MyFileIcon.icns',
                    'LSItemContentTypes': ['com.example.myformat'],
                    'LSHandlerRank': 'Owner'
                    }
                ]
            },
         )

In the above example, the key/value 'NSPrincipalClass': 'NSApplication' is necessary to allow Mac OS X to render applications using retina resolution.

The key 'NSAppleScriptEnabled' is assigned the Python boolean False, which will be output to Info.plist as <false/>. Finally the key CFBundleDocumentTypes tells Mac OS X what file-types your application supports, if any.

like image 199
C. Sederqvist Avatar answered Jan 16 '23 01:01

C. Sederqvist