Victor Domingos
Victor Domingos

Reputation: 1093

How to run a Python 3 tkinter app without opening any Terminal windows in Mac OS X?

I developed a GUI app in Python 3. I want to be able to launch it by clicking on the script itself or in a custom-made launching script. I have changed permissions and it's now opening with "Python Launcher" on double-click. But everytime I run my app, I get a lot of windows:

What is the easiest way to get only the GUI on screen, without any Terminal windows?

Upvotes: 6

Views: 4683

Answers (3)

Seva Alekseyev
Seva Alekseyev

Reputation: 61396

If you wrap a Python script in a MacOS app bundle, the terminal window won't be popping up. The minimal app bundle is a folder with a .app extension, and the following contents:

  • A Contents subfolder
  • An Info.plist file under Contents
  • A MacOS subfolder under Contents
  • The script under MacOS

The script has to contain a hashbang line (#/usr/bin/python3) and have permissions set to 0755.

The Info.plist has to contain the script file name under the CFBundleExecutable key (friendly name Executable file). For compatibility with Apple Silicon Macs, it also needs the LSArchitecturePriority key (Architecture priority) with the first line set to arm64 - without that, Finder will claim this app needs Rosetta. There is a GUI plist editor in Xcode, but just in case you don't know what it is internally, it's an XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key>
    <string>myscript.py</string>
    <key>LSArchitecturePriority</key>
    <array>
        <string>arm64</string>
    </array>
</dict>
</plist>

My rather limited testing shows that no other properties in the app bundle are strictly needed.

Upvotes: 0

Victor Domingos
Victor Domingos

Reputation: 1093

Following the suggestion bu linusg, I discovered that I can accomplish this by creating an AppleScript application with the following code (adjusting the paths of python3 and of the Python application as needed):

do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /usr/local/bin/python3 '/Users/USER/FOLDER/SCRIPT.py' &> /dev/null &"

It launches the Python application without Terminal Windows. The AppleScript application can then be personalised with a custom icon as usual, and can be placed in the Dock. When clicked, it will launch the Python intepreter, that still shows up in Dock, but with no visible windows.

I think this may be useful to other users.

Upvotes: 3

linusg
linusg

Reputation: 6439

By changing the file extension to .pyw (Python Windowed), any terminal/shell/cmd would be hidden by default (I even don't know about the preferences window).

Hope this helps!

Upvotes: 1

Related Questions