Python 101 - How to Create an "exe" for Mac
Each operating system has its own method of installing or running an application. On Windows, you run an executable most of the time. Executables have an extension of .exe. Apple’s Mac software has the concept of applications that use the extension .app. These are technically a bundle of files including a runnable binary. They are kind of like a runnable zip-file. All you need to do is double-click the .app file to run it.
If you want to distribute a Python application on Mac OSX, you have three options:
PyInstaller - https://www.pyinstaller.org/
Briefcase - https://beeware.org/project/projects/tools/briefcase/
You learned about the PyInstaller package in chapter 44. In this chapter you will learn how to use PyInstaller to create an .app bundle.
In this tutorial, you will cover the following two topics:
Installing PyInstaller
Creating an Executable with PyInstaller
Being able to create .app bundles gives to a way to distribute your application to Mac users. Let’s find out how to install the Python packages that you need so you can get started!
Installing PyInstaller
Both PyInstaller and Briefcase can be installed using pip. Here is how you would install PyInstaller:
python -m pip install pyinstaller
This is a pretty quick installation because PyInstaller has a few dependencies. Once PyInstaller is installed, you can move on to the next section and create an executable for Mac OSX.
Creating an Executable with PyInstaller
Creating an executable requires code. You can use a simple wxPython application for this tutorial. Here is the image_viewer program code that you can use to open and display images:
You can use the same commands for PyInstaller that you use on Windows to create a Mac application bundle. Open up a console window on your Mac and navigate to the folder that contains image_viewer.py. Then run the following command:
pyinstaller image_viewer.py --windowed
Running this command will give you this output, which is quite similar to the output you would see on Windows:
Of course, when you run PyInstaller on a Mac, you will see that the platform information reflects that. When this finishes, you will have a dist folder that contains your app bundle. There does seem to be a bug in Python 3.7 and 3.8 that occurs when you try to launch the app bundle:
FileNotFoundError: Tcl data directoryTo fix this issue, you will need to run the following commands in your console:
Now, when you go to run the application bundle, it will launch your wxPython GUI correctly. Because of this issue, if you use the --onefile option, you won’t be able to add those two folders. So the --onefile option won’t actually launch. Instead, you will see it attempt to launch and immediately crash. Hopefully, PyInstaller or a newer version of Python will resolve this issue.
Wrapping Up
Creating executables on macOS is a bit more complicated than on Windows. You also have fewer options for creating application bundles. Windows offers many Python packages to choose from for creating binaries. Mac has PyInstaller and Beeware’s Briefcase. The latter is designed to work with other Beeware products, but can be used with regular Python too.
Go ahead and give them both a try to see what works best for you. PyInstaller is a much more mature option, but sometimes it’s nice to try newer packaging applications.
Get the Book
Did you enjoy this article? Well, it’s from my book, Python 101 - 2nd Edition. Check it out at any of the following websites:




