0

I was trying to convert my python script test.py to a mac app.

I followed this tutorial.

Content of test.py

print("Hello world!")

Then I created setup.py with the following content and saved in to the same folder as test.py:

from setuptools import setup

APP = ['test.py']
OPTIONS = {'argv_emulation': True}

setup(app=APP, options={'py2app':OPTIONS},setup_requires=['py2app'])

Then I navigated my terminal to the folder where test.py and setup.py are stored.

Then I installed py2app with the following command(I am using Pycharm):

pip3 install py2app

Then I entered the following terminal command:

python setup.py py2app

py2app performed multiple operations and responded with

Done!

I navigated back to the directory where test.py and setup.py are stored.

As expected, I found new directories: build and dist.

I opened the dist directory and found a file test.app there.

I double clicked test.app, an icon popped in the dock, bounced for few times, and disappeared.

*I did this with the simplest python script possible in order to figure out what the problem is. I tried to create and app from a more complex script before, in that case after double clicking the {}.app file a window poped app saying

{file name} encountered and error

and let me choose to either terminate the app directly or see the console.

Please what am I doing wrong / is there a straightforward way to turn a py script into a mac app?

Many thanks

Marius
  • 45
  • 1
  • 7
  • It looks like you're trying to make the script interact with the user via a Terminal interface (e.g. using `print()` to send messages to the user, and `input()` to get input). But an app doesn't have a Terminal interface, so this won't work at all. If you want your Python script to function as a GUI app, you need to write it to function via a GUI, not via a Terminal interface. (Either that, or make it interfaceless, and just do things in the background.) – Gordon Davisson Mar 26 '21 at 17:33
  • Thank you. I tried to make the app from another script that uses tkinter and works perfectly when run in Pycharm. After running python setup.py py2app i get: **module 'plistlib' has no attribute 'Dict'** – Marius Mar 27 '21 at 15:51

2 Answers2

0

You did nothing wrong. As there is only a print in test.py, it's normal the app starts and disappears immediately.

You can put in test.py

import tkinter as tk
tk.Frame(tk.Tk()).mainloop()

build app and start it.

Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Thank you for your answer. In order to make the app not disappear immediately after it is initiated, I changed the code in **test.py** to `txt = input("Hey, I will only disappear after you click \'Enter\'")`. I ran the `python setup.py py2app` terminal command again and run the app. What I got now is a dialog saying "test error". – Marius Mar 26 '21 at 11:42
  • Probably because app can not find terminal. Anyway it's a different problem now, maybe post another question ? – Philippe Mar 26 '21 at 11:51
  • If it is certain that the problem is in OS/ terminal configuration, then agreed, it is a different problem. But I am unable to check whether this is the case or whether the py2app does not work properly. As I wrote above, I will be also grateful for any alternative suggestions how to turn a python script into a functional Mac app. I spent hours trying to find a viable solution but nothing seams to work properly. I am starting to think what is the point of learning python if you need a computer science phd to turn it into an app that actually works. Writing scripts rocks, but distribution too. – Marius Mar 26 '21 at 12:00
  • The `app` in my answer actually works. Have you tried that ? – Philippe Mar 26 '21 at 12:14
  • I tried and (at least on my mac) it crashes immediately. As per below, it really appears to be a py2app issue. Some of them can be solved by downgrading py2app, for some of them I have not found any solution so far. I will keep looking, in either case, many thanks for taking the time to discuss this. Take care – Marius Mar 27 '21 at 15:58
  • The solution I gave worked on my MacOS/Big Sur. Can you double check the `pip3` matches `python` you used ? `python -V` and `pip3 -V` – Philippe Mar 27 '21 at 19:17
  • Hey I had no idea the problem may be there. I checked pip3 and python versions as you suggested and the outcome is as follows: **pip3-V**: pip 21.0.1 from /Users/{my_sys_login}/{my_project_name}/lib/python3.9/site-packages/pip (python 3.9), **python-V**: Python 3.9.2 – Marius Mar 28 '21 at 17:08
0

After further googling I came across this post that discusses this problem. No that it would solve my problem, but maybe it will be helpful to somebody else.

Marius
  • 45
  • 1
  • 7