PyInstaller icon option doesnt work on Mac

2024/11/14 23:38:54

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?

Answer

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.

https://en.xdnf.cn/q/71502.html

Related Q&A

Error Installing scikit-learn

When trying to install scikit-learn, I get the following error:Exception:Traceback (most recent call last):File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2…

Issues downloading Graphlab dependencies get_dependencies()

I am having trouble when I try to download the dependencies needed to run graphlab. I do import graphlab I get the following:ACTION REQUIRED: Dependencies libstdc++-6.dll and libgcc_s_seh-1.dll not fou…

Django Tastypie slow POST response

Im trying to implement a Tastypie Resource that allows GET & POST operations following a per user-permission policy, the model is pretty simple (similar to the Note model in Tastypie documentation)…

Extract a region of a PDF page by coordinates

I am looking for a tool to extract a given rectangular region (by coordinates) of a 1-page PDF file and produce a 1-page PDF file with the specified region:# in.pdf is a 1-page pdf file extract file.pd…

Is it possible to concatenate QuerySets?

After a search of a database I end up with an array of querysets. I wanted to concatenate these queryset somewhat like we can do with list elements. Is this possible or maybe there an altogether better…

Pickling a Python Extension type defined as a C struct having PyObject* members

I am running C++ code via Python and would like to pickle an extension type.So I have a C++ struct (py_db_manager) containing pointers to a database object and a object manager object (both written in …

Generating random ID from list - jinja

I am trying to generate a random ID from a list of contacts (in Python, with jinja2) to display in an HTML template. So I have a list of contacts, and for the moment I display all of them in a few cell…

Unit testing Flask app running under uwsgi

I’m relatively new to python and am looking for a pythonic way to handle this practice. I’ve inherited a fairly trivial Python 2.7 Flask app that runs under uwsgi that I want to add some unit tests t…

fastest way to find the smallest positive real root of quartic polynomial 4 degree in python

[What I want] is to find the only one smallest positive real root of quartic function ax^4 + bx^3 + cx^2 + dx + e [Existing Method] My equation is for collision prediction, the maximum degree is quarti…

Split strings by 2nd space

Input :"The boy is running on the train"Output expected:["The boy", "boy is", "is running", "running on", "on the", "the train"]Wha…