Let’s say you finished up a wonderful GUI application using wxPython. How do you share it with the world? This is always the dilemma when you finish an amazing program. Fortunately, there are several ways you can share your code. If you want to share your code with other developers, than Github or a similar website is definitely a good way to do. I won’t be covering using Git or Mercurial here. Instead what you will learn here is how to turn your application into an executable.
By turning your code into an executable, you can allow a user to just download the binary and run it without requiring them to download Python, your source code and your dependencies. All of those things will be bundled up into the executable instead.
There are many tools you can use to generate an executable:
You will be using PyInstaller in this tutorial. The main benefit to using PyInstaller is that it can generate executables for Windows, Mac and Linux. Note that it does not support cross-compiling. What that means is that you cannot run PyInstaller on Linux to create a Windows executable. Instead, PyInstaller will only create an executable for the OS that it is ran on. In other words, if you run PyInstaller on Windows, it will create a Windows executable only.
Installing the PyInstaller package is nice and straightforward. All you need is pip.
Here is how you would install PyInstaller to your system Python:
pip install pyinstaller
You could also install PyInstaller to a virtual Python environment using Python’s venv module or the virtualenv package.
Generating an Executable
The nice thing about PyInstaller is that it is very easy to use out of the box. All you need to do is run the `pyinstaller` command followed by the path to the main file of the application that you want to convert to an executable.
Here is a non-working example:
If the PyInstaller application is not found, you may have to specify a full path to it. By default, PyInstaller installs to Python’s **Scripts** sub-folder, which is going to be in your system Python folder or in your virtual environment.
Let’s take one of the simple applications from my upcoming book and turn it into an executable. For example, you could use image_viewer_slideshow.py from chapter 3:
def __init__(self, parent):
self.max_size = 240
self.photos = 
self.current_photo = 0
self.total_photos = 0
self.slideshow_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_next, self.slideshow_timer)
Layout the widgets on the panel
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
img = wx.Image(self.max_size, self.max_size)
self.image_ctrl = wx.StaticBitmap(self, wx.ID_ANY,
self.main_sizer.Add(self.image_ctrl, 0, wx.ALL|wx.CENTER, 5)
self.image_label = wx.StaticText(self, label="")
self.main_sizer.Add(self.image_label, 0, wx.ALL|wx.CENTER, 5)
btn_data = [("Previous", btn_sizer, self.on_previous),
("Slide Show", btn_sizer, self.on_slideshow),
("Next", btn_sizer, self.on_next)]
for data in btn_data:
label, sizer, handler = data
self.btn_builder(label, sizer, handler)
self.main_sizer.Add(btn_sizer, 0, wx.CENTER)
def btn_builder(self, label, sizer, handler):
Builds a button, binds it to an event handler and adds it to a sizer
btn = wx.Button(self, label=label)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
def on_next(self, event):
Loads the next picture in the directory
if not self.photos:
if self.current_photo == self.total_photos - 1:
self.current_photo = 0
self.current_photo += 1
def on_previous(self, event):
Displays the previous picture in the directory
if not self.photos:
if self.current_photo == 0:
self.current_photo = self.total_photos - 1
self.current_photo -= 1
def on_slideshow(self, event):
Starts and stops the slideshow
btn = event.GetEventObject()
label = btn.GetLabel()
if label == "Slide Show":
def update_photo(self, image):
Update the currently shown photo
img = wx.Image(image, wx.BITMAP_TYPE_ANY)
# scale the image, preserving the aspect ratio
W = img.GetWidth()
H = img.GetHeight()
if W > H:
NewW = self.max_size
NewH = self.max_size * H / W
NewH = self.max_size
NewW = self.max_size * W / H
img = img.Scale(NewW, NewH)
img = wx.Image(self.max_size,
bmp = wx.Bitmap(img)
self.current_photo = 0
self.photos = 
super().__init__(None, title='Image Viewer',
self.panel = ImagePanel(self)
Create a toolbar
self.toolbar = self.CreateToolBar()
open_ico = wx.ArtProvider.GetBitmap(
wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (16,16))
openTool = self.toolbar.AddTool(
wx.ID_ANY, "Open", open_ico, "Open an Image Directory")
self.Bind(wx.EVT_MENU, self.on_open_directory, openTool)
def on_open_directory(self, event):
Open a directory dialog
with wx.DirDialog(self, "Choose a directory",
style=wx.DD_DEFAULT_STYLE) as dlg:
if dlg.ShowModal() == wx.ID_OK:
self.folderPath = dlg.GetPath()
photos = glob.glob(os.path.join(self.folderPath, '*.jpg'))
self.panel.photos = photos
self.panel.total_photos = len(photos)
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainFrame()
If you wanted to turn it into an executable, you would run the following:
Make sure that when you run this command, your current working directory is the one that contains the script you are converting to an executable. PyInstaller will be creating its output in whatever the current working directory is.
When you run this command, you should see something like this in your terminal:
PyInstaller will create two folders in the same folder as the script that you are converting called **dist** and **build**. The **dist** folder is where you will find your executable if PyInstaller completes successfully. There will be many other files in the **dist** folder besides your executable. These are files that are required for your executable to run.
Now let’s try running your newly created executable. When I ran my copy, I noticed that a terminal / console was appearing behind my application.
Image Viewer with Console in Background
This is normal as the default behavior of PyInstaller is to build your application as if it were a command-line application, not a GUI.
You will need to add the –noconsole flag to remove the console:
pyinstaller image_viewer_slideshow.py --noconsole
Now when you run the result, you should no longer see a console window appearing behind your application.
It can be complicated to distribute lots of files, so PyInstaller has another command that you can use to bundle everything up into a single executable. That command is `–onefile`. As an aside, a lot of the commands that you use with PyInstaller have shorter aliases. For example, there is a shorter alias for `–noconsole` that you can also use called: -w. Note the single dash in `-w`.
So let’s take that information and have PyInstaller create a single file executable with no console:
pyinstaller image_viewer_slideshow.py --onefile -w
You should now have just one file in the dist folder.
The spec file
PyInstaller has the concept of specification files. They are kind of like a setup.py script, which is something that you use with Python’s distutils. These spec files tell PyInstaller how to build your executable. PyInstaller will generate one for you automatically with the same name as the passed in script, but with a .spec extension. So if you passed in image_viewer_slideshow.py, then you should see a image_viewer_slideshow.spec file after running PyInstaller. This spec file will be created in the same location as your application file.
Here is the contents of the spec file that was created from the last run of PyInstaller above:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['image_viewer.py'],
pyz = PYZ(a.pure, a.zipped_data,
exe = EXE(pyz,
While PyInstaller worked fine with the image viewer example, you may find that it won’t work out of the box if you had other dependencies, such as NumPy or Pandas. If you run into issues with PyInstaller, it has very verbose logs that you can use to help you figure out the issue. One good location is the `build/cli/warn-cli.txt` file. You may also want to rebuild without the `-w` command so that you can see what is being printed to stdout in the console window.
There are also options for changing the log level during building that may help you uncover issues.
If none of those work, try Google or go to PyInstaller’s support page and get help there.
Creating Executables for Mac
While the same commands should work on Mac OSX as it does on Windows, I found that I needed to run the following command to generate a working executable:
pyinstaller image_viewer_slideshow.py --windowed
The output that PyInstaller generates will be slightly different and the result is an application file.
Another popular option for generating applications on Mac is a Python package called py2app.
Creating Executables for Linux
For Linux, it is usually recommended that you build the executable with an old version of glibc because the newer glibc versions are backwards compatible. By building with an old version of Linux, you can usually target a wider variety of Linux versions. But your mileage may vary.
After the files are generated, you can just tar them up into a gzipped tarball (.tax.gz). You could even using the archiving application you created in this book to do that for you, if you wanted.
An alternative would be to learn how to create a .deb or related file that most Linux versions can install.
Learning More About PyInstaller
This article is not meant to be an in-depth guide to PyInstaller. It will likely change much faster than wxPython, so it is recommended that you read the documentation for PyInstaller instead. It will always be the most up-to-date location to get the information you need on the project.
What About Installers?
Windows users know that most of the time you have an installer application that you can run to install your application on your computer and put some shortcuts here and there. There are several useful free programs that you can use to create a Windows Installer as well as some paid ones
Here are the two freeware applications I see mentioned the most:
I have used Inno Setup to create a Windows installer on several occasions. It is easy to use and requires only a little reading of its documentation to get it working. I haven’t used NSIS before, but I suspect it is quite easy to use as well.
Let’s use Inno Setup as an example and see how to generate an installer with it.
Creating an Installer with Inno Setup
Inno Setup is a nice freeware application that you can use to create professional looking installer programs. It works on most versions of Windows. I personally have used it for quite a few years. While Inno Setup is not open source, it is still a really nice program. You will need to download and install it from there website.
Once installed, you can use this tool to create an installer for the executable you created earlier in this chapter.
To get started, just run Inno Setup and you should see the following:
Inno Setup’s Startup Page
While Inno Setup defaults to opening an existing file, what you want to do is choose the second option from the top: “Create a new script file using the Script Wizard”. Then press **OK**.
You should now see the first page of the Inno Setup Script Wizard. Just hit **Next** here since there’s nothing else you can really do.
Now you should see something like this:
Inno Setup Script Wizard Application Information Page
This is where you enter your applications name, its version information, the publisher’s name and the application’s website. I pre-filled it with some examples, but you can enter whatever you want to here.
Go ahead and press Next and you should see page 3:
Inno Setup Script Wizard Application Folder Page
This page of the wizard is where you can set the application’s install directory. On Windows, most applications install to **Program Files**, which is also the default here. This is also where you set the folder name for your application. This is the name of the folder that will appear in Program Files. Alternatively, you can check the box at the bottom that indicates that your application doesn’t need a folder at all.
Let’s go to the next page:
Inno Setup Script Wizard Application Files Page
Here is where you will choose the main executable file. In this case, you want to choose the executable you created with PyInstaller. If you didn’t create the executable using the –onefile flag, then you can add the other files using the Add file(s)… button. If your application requires any other special files, like a SQLite database file or images, this is also where you would want to add them.
By default, this page will allow the user to run your application when the installer finishes. A lot of installers do this, so it’s actually expected by most users.
Inno Setup Script Wizard Application Shortcuts Page
This is the Application Shortcuts page and it allows you to manage what shortcuts are created for your application and where they should go. The options are pretty self-explanatory. I usually just use the defaults, but you are welcome to change them however you see fit.
Let’s find out what’s on the documentation page:
Inno Setup Script Wizard Application Documentation Page
The Documentation Page of the wizard is where you can add your application’s license file. For example, if you were putting out an open source application, you can add the GPL or MIT or whatever license file you need there. If this were a commercial application, this is where you would add your End-Users License Agreement (EULA) file.
Let’s see what’s next:
Inno Setup Script Wizard Setup Languages Page
Here you can set up which setup languages should be included. Inno Setup supports quite a few languages, with English as the default choice.
Now let’s find out what compiler settings are:
Inno Setup Script Wizard Compiler Settings Page
The Compiler Settings page let’s you name the output setup file, which defaults to simply **setup**. You can set the output folder here, add a custom setup file icon and even add password protection to the setup file. I usually just leave the defaults alone, but this is an opportunity to add some branding to the setup if you have a nice icon file handy.
The next page is for the preprocessor:
Inno Setup Script Wizard Preprocessor Page
The preprocessor is primarily for catching typos in the Inno Setup script file. It basically adds some helpful options at compile time to your Inno Setup script.
Check out the documentation for full details.
Click Next and you should see the last page of the wizard:
Inno Setup Script Wizard End Page
Click Finish and Inno Setup will generate an Inno Setup Script (.iss) file. When it is finished, it will ask you if you would like to compile the file.
Go ahead and accept that dialog and you should see the following:
Inno Setup Script
This is the Inno Setup Script editor with your newly generated script pre-loaded into it. The top half is the script that was generated and the bottom half shows the compiler’s output. In this screenshot, it shows that the setup file was generated successfully but it also displays a warning that you might want to rename the setup file.
At this point, you should have a working installer executable that will install your program and any files it depends on to the right locations. It will also create shortcuts in the Windows Start menu and whichever other locations you specified in the wizard.
The script file itself can be edited. It is just a text file and the syntax is well documented on Inno Setup’s website.
Windows and Mac OSX prefer that applications are signed by a corporation or the developer. Otherwise you will see a warning that you are using an unsigned piece of code or software. The reason this matters is that it protects your application from being modified by someone else. You can think of code signing as a kind of embedded MD5 hash in your application. A signed application can be traced back to whomever signed it, which makes it more trust-worthy.
If you want to sign code on Mac OSX, you can use XCode
Windows has several options for signing their code. Here is a URL for getting your application certified for Windows
You can also purchase a certificate from various companies that specialize in code signing, such as digicert.
There is also the concept of self-signed certificates, but that is not for production or for end users. You would only self-sign for internal testing, proof-of-concept, etc. You can look up how to do that on your own.
You have now learned how to generate executables using PyInstaller on Windows, Mac and Linux. The command to generate the executable is the same across all platforms. While you cannot create a Windows executable by running PyInstaller on Linux, it is still quite useful for creating executable for the target operating system.
You also learned how to use Inno Setup to create an installer for Windows. You can now use these skills to create executables for your own applications or for some of the other applications that you created in this book!
A bbfreeze Tutorial – Build a Binary Series!
Packaging wxPyMail for Distribution