In this tutorial we’ll be using py2app to create a standalone OSX application from a Python 2 or 3 source code with asimple Tkinter user interface.

  1. Installing Python On Mac Os
  2. Python Mac Os App
  3. Python Install Mac Os

Download Python Runner for macOS 10.9 or later and enjoy it on your Mac. ‎Write and run Python code instantly! Python Runner is a handy tool for learning Python or running Python script for daily tasks. Installing Python 2 on Mac OS X. Strongly recommend that you install the tools and libraries described in the next section before you start building Python applications for real-world use. In particular, you should always install Setuptools, as it makes it much easier for you to install and manage other third-party Python libraries.

'py2app is a Python setuptools command which will allow you to make standalone application bundles and plugins from Python scripts. py2app is similar in purpose and design to py2exe for Windows.'

Installing Python On Mac Os

Relevant links about py2app:

  • Source on BitBucket (last commit 2015-05-05)
  • Issue Tracker, Mailing List

This guide is loosely based on the official tutorial.Based on a Python file called Sandwich.py, we’ll create an application called Sandwich.app.

Prerequisites

Create a custom directory and create a virtualenv:

Now create a very simple Tkinter app with the filename Sandwich.py:

This little app will look like this:

Install py2app

The original version of py2app has a bug due to a newer version of ModuleGraph. Imade a fork of the project and fixed this bug on Github.Install it with pip like this:

Create a setup.py file

py2app includes py2applet, a helper which generates a setup.py file for you:

This setup.py is a basic definition of the app:

If your application uses some data files, like a JSON, text files or images, you should include them in DATA_FILES. For example:

Build the app for development and testing

py2app builds the standalone application based on the definition in setup.py.

For testing and development, py2app provides an “alias mode”, which builds anapp with symbolic links to the development files:

This creates the following files and directories:

This is not a standalone application, and the applications built in alias mode are not portable to other machines!

The app built with alias mode simply references the original code files, so any changes you make to the original Sandwich.py file are instantly available on the next app start.

The resulting development app in dist/Sandwich.app can be opened just like any other .app with the Finderor the open command ($ open dist/Sandwich.app). To run your application directly from the Terminalyou can just run:

Building for deployment

When everything is tested you can produce a build for deployment with a calling python setup.py py2app. Make sure that any old build and dist directories are removed:

This will assemble your application as dist/Sandwich.app. How to support cleaner app on mac. Since this application is self-contained, you will have to run the py2app command again any time you change any source code, data files, options, etc.

The original py2app has a bug which would display “AttributeError: 'ModuleGraph' object has no attribute 'scan_code'” or load_module. If you encounter this error, takea look at this StackOverflow thread or use my fork of py2app.

The easiest way to wrap your application up for distribution at this point is simply to right-click the application from Finder and choose “Create Archive”.

Adding an icon

Simply add 'iconfile': 'youricon.icns' to the OPTIONS dict:

You can find free icons in icns format around the web (eg. on IconFinder or freepik).

Advanced app settings

You can tweak the application information and behaviour with modificationsto the Info.plist. The most complete reference for the keys available is Apple’s Runtime Configuration Guidelines.

Here is an example with more modifications:

With these settings, the app will have the following infos:

References

  • py2app documentation, examples
  • Tkinter, Tkinter resource collection, An Introduction to Tkinter

See Also

  • PyInstaller - Another tool to create cross-platform standalone apps (libraries like PyQt, Django or matplotlib are fully supported)
  • rumps - Ridiculously Uncomplicated Mac OS X Python Statusbar Apps
  • py2exe - same as py2app but for Windows .exe files
  • cx_Freeze - Another packager to create Windows .exe files

If you have suggestions, feedback or ideas, please reach out to me @metachris.

We will explore the use of PyInstaller to create a cross-platform standalone application using python. For this tutorial I will be using Tkinter to visually demonstrate.

PyInstaller can be used in Python3 and Python2, similarly it creates applications for MacOS, Windows, and Linux.

I will be using:

  • Python 3
  • MacOS 10.12.6
  • PyInstaller 3.5

N.B. - Currently Python 3 has issues with importing a compatible version of tcl on MacOS, however the below describes a method that works around this.

Android app maker mac. Editing Java codeFirst, install the. You will also get IntelliSense assistance, such as Member List, Parameter Help, Quick Info, making writing Java code more efficient. Debugging Java codeTo turn on Java debugging for your Android projects in your next debugging session, in the Debug Target toolbar, change Debug Type dropdown to “ Java Only” as shown in the following screenshot.Now you can set line breakpoints, including conditions or hit counts for the breakpoints, anywhere in the Java code. In the following screenshot, Visual Studio provides a member list for the android.util.Log class.Another handy feature for larger codebases or for navigating 3rd party libraries for which you have the source code available is Go to definition (F12) which will take you to the symbol definition location if available. It provides colorization (both syntactic and semantic), error and warning squiggles as well as code outlining and semantic highlighting in your Java files.

Setup

PyInstaller runs from within the terminal, from here we will install PyInstaller, create a working directory, and cd to it.

Write your python script

This will vary depending on your task, below I will demonstrate visually by creating a tkinter app.

If needed: pip install tkinter.

Save the below tkinter app as DogsGo.py.

Run the above $ python DogsGo.py or from within python.

You should have generated an app that shows:

Python Mac Os App

N.B. Python 2 uses import Tkinter and Python 3 import tkinter.

Build the application

Before we build the application you should first understand flags, these are settings which you apply at the time of building. In many cases these can be applied after, however many are easiest to add at the point of build. For this example I will describe two, however more can be found here.

-w, –windowed, –noconsole - Prevents terminal from appearing when app is opened.

Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS X this also triggers building an OS X .app bundle. On Windows this option will be set if the first script is a ‘.pyw’ file. This option is ignored in *NIX systems.

-F, –onefile - App is bundled into a single file.

Create a one-file bundled executable.

If you are a MacOS user don’t use –onefile, I’ll elaborate below.

Python Install Mac Os

Windows and Linux

Once the build is complete, navigate to dist and run the DogsGo application. Or:

*change extension and path depending on OS.

MacOS

Currently the pyinstaller build on MacOS uses an incompatible version of tcl, resulting in the app immediately crashing when it opens (i.e. if you follow the above). Below is a method which allows the application to be opened by editing the init.tcl file. I have written a python script which makes these changes.

The build will be created however the Contents of the application will remain as separate files. Clone the TCLChanger.py from my git repository and run in the same working directory as your original python script.

Or download the file if you don’t use git :’(

You should now be able to open the application.

Exploring .spec and adding an icon

Adding an application icon can be done using the flag --icon=icon-name.icns during the build, however we will use this as an oppertunity to delve into the .spec file that has been generated.

Opening DogsGo.spec will show a number of settings we can update, for the application icon we want to update exe = EXE(..) and app = BUNDLE(..).

Let us update the icon to dog.icns, don’t forget to rename it dog.icns and copy it to your current directory.

exe = EXE(…)

Add icon='dog.icns'.

app = BUNDLE()

Change icon=None to icon='dog.icns'.

Now that DogsGo.spec has been updated, run the pyinstaller for the DogsGo.spec.

MacOS users, remember to run$ python TCLChanger.pyagain

Final Note

That’s it! You will now be able to transfer the standalone application and use it.

woof.

Helpful links

  • Pyinstaller Manual: https://pyinstaller.readthedocs.io/en/stable/index.html
  • TKDocsL: https://tkdocs.com/index.html
  • PyInstaller repository: https://github.com/pyinstaller/pyinstaller
  • TCLChanger Repository: https://github.com/jacob-brown/TCLChanger
Coments are closed
Scroll to top