How to create a Mac OS X app with Python?
Asked Answered
I

11

41

I want to create a GUI application which should work on Windows and Mac. For this I've chosen Python.

The problem is on Mac OS X.

There are 2 tools to generate an ".app" for Mac: py2app and pyinstaller.

  1. py2app is pretty good, but it adds the source code in the package. I don't want to share the code with the final users.
  2. Pyinstaller generates UNIX executable, so how to run it on Mac? I created a bundles with this executable, but the resulted ".app" is not working.

The questions are:

  1. How to configure py2app to include the source code in the executable, so the final users will not have access to my program?
  2. How to convert UNIX executable to Mac ".app" ?
  3. Is there a way to compile Python code with GCC ?
  4. In Windows it's easy, I created an "exe" file from Python code and it works. Is it possible to create a single file "app" for Mac ?

P.S. I use two computers (Windows and for Mac), Python 2.7, wxPython, py2exe, py2app and pyinstaller.

Also, I have checked out these sites:

Idell answered 31/8, 2011 at 18:39 Comment(1)
Obviously almost everything in computers can in some way be reverse engineered or decompiled. I think he means a way to compile the program so that the source code would not stand there just waiting to be read. In windows you can build a nice stand alone exe. Something like that would do. Most likely 99% of the generic users will never even imagine taking a peek inside, but for the 1% who do it would ensure only .1% actually have the skills to pull it off, not have it served on a platter.Excrescence
C
11

How to configure py2app to include the source code in the executable, so the final users will not have access to my program?

Unless you very seriously hack the python interpreter (and include the mangled version) there is no really good way to hide the source from a moderately skilled and determined user. I strongly believe this is true on Windows also. Basically, whether you include true source or bytecode, a pretty clean version of the source can be recovered. More importantly, in my opinion, unless you include the actual source code (as opposed to bytecode, you will introduce a possible dependency on the interpreter version).

How to convert UNIX executable to Mac ".app" ?

What do you mean by a UNIX executable? A Darwin (OS X) binary [which isn't actually UNIX]? That can be done using the kinds of tools you already mentioned, but it must be done carefully to avoid library dependencies.

If all you want it a simple wrapper to put a command-line binary into a window, it's pretty easy to accomplish and the free XCode suite has several examples that would serve (depending on what output you wan to deliver, if any).

Is there a way to compile Python code with GCC ?

GCC does not compile Python. It's a different language (although there tools in the gcc family rthat support multiple language front-ends, but not Python). There are tools that attempt to translate Python into C, and then you can compile that into a true binary, but this only works for programs that avoid certain types of construct, and the process (and restrictions) need to apply your libraries as well.
One project to allow this is Cython. It works well for some types of code, mostly numerical code, but it is not trivial to install and exploit, very especially if you want to produce something that runs on multiple different computers.

In Windows it's easy, I created an "exe" file from Python code and it works. Is it possible to create a single file "app" for Mac ?

I would have to say I am skeptical -- very skeptical -- about this. Just like the OS X case, the exe almost certainly has the source code trivially accessible within it.

One fairly easy trick is to encrypt the source code and then decrypt it on the fly, but this seems to me like more trouble than it's worth.

Cowling answered 17/2, 2013 at 21:14 Comment(2)
Most of these tools only embed bytecode, which is not that easy to reverse-engineer. It's definitely possible, and easier than reverse-engineering compiled C (though not as easy as reversing unobfuscated Java or C#)Milkandwater
Reverse engineering bytecode is pretty easy, in my opinion. You don't get attractive maintainable code, but you expose any secrets. Also, some of these tools include true source code. Decompiling compiled code (like C) for reverse engineering is essentially impossible in practice (Yes, that is a generalization: I have done it successfully myself many times, and realize it can be done, but it's qualitatively different and only works in special cases).Cowling
G
7

PyInstaller will automatically create bundles under Mac OSX for windowed executables. When running ypinstaller.py, make sure to pass the option "--windowed".

This feature is documented in the website of pyinstaller

Gurevich answered 30/10, 2013 at 2:58 Comment(3)
This solves my problem: it doesn't include my python source code unlike py2app does.Inoculum
Here's the docs: pythonhosted.org/PyInstaller/#building-mac-os-x-app-bundlesTransilluminate
PyInstaller puts the byte code into the executable, which can be recovered trivially using "pyinstaller extractor". Then used decompyle3 to get back source code.Cowling
P
6

If you're not completely committed to wxPython (and for anyone else looking for a cross platform Python GUI framework), I recommend you check out Kivy. It's cross platform, GPU accelerated, and it will do the app packaging for you. It's easy to jump into, has a well thought-out architecture, and gives you an incredible amount of flexibility in terms of the interface. It's the best way I've found to make a cross platform Python GUI app.

Parsley answered 23/2, 2013 at 4:22 Comment(4)
But what about its desktop GUI native support? I believe it doesn't offer them. e.g, it didn't provide what we already knew as Combo Box.. only Spinner.Ileac
@Ileac Kivy won't have the same look as native controls because it has its own set of widgets. But that's a common tradeoff when you're coding cross platform GUIs.Parsley
I guess you were right. But in this manner, PyQt is more suited to the overall "nativeness" when doing cross platform GUIIleac
@Ileac True, Qt does nativeness exceptionally well.Parsley
F
5

cxFreeze was the choice. I use it pack my python program to a Mac OS X app. Which works like a charm.

Festus answered 21/5, 2013 at 16:15 Comment(0)
R
3

Automator was already mentioned as a quick and simple solution for Pythons scripts that are contained in a single file, but since the Automator UI has so many options, it might not be obvious how to actually do it, so here are the steps:

  1. In Automator select File > New and pick Application as document type.
  2. Next, make sure Actions tab is selected on the left, and then in the search box type run. Among other options you'll see Run Shell Script — doubleclick it, and an editor window will appear in the right panel.
  3. From the Shell dropdown menu select /usr/bin/python, /usr/bin/python3 or whatever other python you want to use.
  4. Paste your Python code into the edit window and then pick File > Save.

By default, the app will be saved under $HOME/Applications and will appear in Spotlight.

If you want to be able to set your own icon and have some fancy features, like task bar icons with a menu, log windows etc, then have a look at Platypus — an open-source app for creating MacOS native bundles.

Religiosity answered 26/7, 2015 at 15:44 Comment(0)
I
1

2: You can't "convert" it, but you can move the executable to App.app/Contents/MacOS/something in a .app file, with CFBundleExecutable set to "something". This would not generally be recommended.

Insula answered 17/10, 2012 at 16:4 Comment(0)
U
0

A motivated person could probably reconstruct usable source code from the Python bytecode in your app, so you might reconsider your opposition to py2app. If you don't trust your final users, why are you doing business with them?

Unbidden answered 31/8, 2011 at 19:14 Comment(0)
C
0

Having used py2exe for windows users so they wouldn't have to deal with library versions, I've torn apart the compiled programs, they include the python bytecode files. While you can make it a violation of the license to look inside those, the fact is that if a computer can execute them, I can read them. It is possible to compile python programs with gcc, via a C preprocessor (try looking for 2c.py on google), I don't know if any of them support GCC. Again, you don't gain any security through using them, but you can get a significant speed improvement.

Cascio answered 29/9, 2012 at 2:10 Comment(0)
F
0

I haven't tried it with big Python projects, but for my own scripts, the easiest way I found was to use Automator

You can interactively create an app project with Run Shell Script action, then paste in your script in its editor, select your shell program (/usr/bin/python), finally save the project. And you have yourself a Mac native app.

Automator can also be driven by AppleScript. So you can pipeline this py-2-app conversion process to your build scripts.

I've never tested a GUI program with it so I don't know if you'll be happy with it. But I'd give it a try since you may wonder how well all the cited 3rd-party python modules/applications are maintained, and how long they are gonna last. Coming bundled with OS X, Automator will likely stay, unless Apple got REALLY tired of it.

Fronde answered 16/2, 2014 at 16:32 Comment(0)
Y
0

cxFreeze is best solution available, first create your program or application using python and than make setup file for your application, and than build the app using build command python setup.py build, according to your requirement you need to make some changes.

Yellow answered 28/11, 2018 at 11:40 Comment(0)
E
-1

The only way is py2app. You have no other way. Sorry. The research you did seems very solid and you did not miss anything.

Excrescence answered 18/9, 2012 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.