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.
'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.'
Loadable bundles are bundles which contain code that can be loaded at runtime. Loadable bundles usually have the extension.bundle, and are most often used as plug-ins. On macOS, there is a way to load bundles even into applications that do not support them, allowing for third party hacks for popular applications, such as Safari and Apple Mail. Py2app - Create standalone Mac OS X applications with Python ¶. Py2app - Create standalone Mac OS X applications with Python. 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.
A XUL application for Mac OS X should follow something similar to this basic directory structure: Example.app/ Contents/ Info.plist (Application bundle properties are specified in this XML file.) PkgInfo (This is a simple text file and is created along with the Info.plist file) MacOS/ (The MacOS folder will contain your XULRunner executable). Kindle Create finds and styles your chapter titles automatically while simultaneously building your table of contents. See your book as your readers do Quickly review your book with built in Kindle Previewer and see how it will look to readers using phones, tablets and eReaders.
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: Google apps on mac desktop.
Create App Bundle Macos 10.13
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: How to add pictures to photos on mac.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:![Macos Macos](/uploads/1/3/4/1/134119384/734647565.png)
This will assemble your application as
dist/Sandwich.app
. 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.
Mac OS X applications are typically packaged as application bundles which are then distributed in .dmg (disk image) files. This tutorial will show you how to package your XULRunner-based application in a way which is consistent with the expectations of Mac OS X users.
Application bundle layout
Application bundles are essentially just directory structures that conform to a standard layout and naming convention. A XUL application for Mac OS X should follow something similar to this basic directory structure:
Example.app/
Contents/
Info.plist
(Application bundle properties are specified in this XML file.)PkgInfo
(This is a simple text file and is created along with the Info.plist file)MacOS/
(The MacOS folder will contain your XULRunner executable)xulrunner
(This is the xulrunner stub)
Resources/
(This is where you place your XUL Application code and support files)application.ini
(XULRunner-related application settings)example.icns
(This is the icon which will be used by your application bundle)chrome/
content/
example.xul
(This directory contains your application's chrome)
defaults/
preferences/
app-prefs.js
(This provides some default values for preferences)
Application bundle contents
In addition to the standard directory hierarchy that's required of all Mac OS X applications, as shown above in Application bundle layout, there are some specific rules for what content goes where:
- The top-level directory is given a name that ends with
.app
which designates the whole package as an application bundle. - The first sub-directory is called
Contents
and within that directory you will place several files and folders that are needed by your application bundle. Info.plist
andPkgInfo
must exist in the Contents directory. The content of these files is covered in the article Deploying XULRunner 1.8.
Examples
These example files are taken from a port of WebRunner which was created to illustrate one method of Mac OS X application packaging.