Making my python program a real application

482 views Asked by At

Using the simple instructions here https://www.metachris.com/2015/11/create-standalone-mac-os-x-applications-with-python-and-py2app/

I now have an app file in dist. However, when I open it, I get an alert saying nothing other than

App Error, Open Console or Terminate

My program is a python file that imports many modules (opencv, tkinter, and much more). When I run it from console (python app.py) it works great, opens my tkinter window, etc.

So firstly, what may be going wrong? And also, how is this actually supposed to work, does the app include all opencv too? How do python modules get included in a portable app like this?

Thanks for any help / guidance.

1

There are 1 answers

5
Dalen On BEST ANSWER

That's more than one Q!

Never mind.

  1. When you are building an app using Python interpreter provided by Apple you get semi-standalone application.

When such application is started it runs built-in Python from Apple and uses its standard library.

Any non-standard module should be included, but problems are often encountered.

  1. When you install your Python from python.org and all additional libs into it, then you proceed making app with it, instead with Apple's build of Python,

then whole interpreter is packed up, along with the standard lib, and anything else you imported.

This often works like a charm, although it produces much bigger apps. But now a days, its not such a catastrophy.

How does it work? py2app, py2exe and similar tools inspect the python code tree for import statements.

Then all dependencies found are included into the package.

Problematic part are dynamic imports that use import() or reload() function. For such modules you have to specifically tell py2app to package them.

Sometimes it is enough to add a static import to it in your app like:

if 0:
    import site

OpenCV, hm, I wish you luck! It brings living nightmares sometimes to properly link its paths for C++, I hope packaging it into app along with Python will work.

I do not use it, so I wouldn't know how much trouble are you in.

Use /Applications/Utilities/Console.app to see any errors and tracebacks sent to stderr/stdout after your app is packaged. You might find some useful info there.

But I'd advise you to download custom Python interpreter and make full standalone app. It's not a big deal. Just call setup.py with it instead with built-in one.

And search around on what you have to add to your setup.py to package OpenCV properly, if it is the cause of your trouble.