Making a Stand Alone Executable from a Python Script using PyInstaller
5 stars based on
Python is one of my favorite programming languages. That being said, if you've ever had to deploy an application written in Python then you know just how painful it can be. Fortunately, there are some pretty awesome open-source tools that can be used to package a Python program into a standalone binary executable that contains everything needed to run the application i.
Python interpreter, program code, libraries, data, etc. In this article, I'll show you how to create a binary executable version of a graphical "Hello World" application using PyInstaller on Windows. Of course, make sure that you already have Python 2. The demo app in this article uses the wxPython library, so you will need to install that if you plan to follow along, but it is not necessary for using PyInstaller.
This app will be a simple "Hello World" graphical app. Save the source code below as app. The source code shown above was taken from the wxPython Getting Started guide, which you may how can i create a stand-alone binary from a python script interested in reading if you want to create graphical Python applications. PyInstaller can be installed using Pipthe Python package manager. If the build was successful, the final executable, app. See the PyInstaller Manual for more configuration information.
You do not need to specify additional modules in the command as they will be automatically pulled via import statements. On my system the final executable is a sizable 8. The executable is relatively large because the Python interpreter, the application code, and all the required libraries are all packaged in as specified by the --onefile option.
Though convenient, there are some implications with this approach which you should be aware of before releasing using this method. See the PyInstaller Manual for more information on bundling. After the build, an app. This file contains all of the options used to run PyInstaller, and can be fed back into PyInstaller for future builds in place of the command line options, if desired. IconEden offers some nice royalty-free icons, so I'll use one of theirs for this demo.
This is how can i create a stand-alone binary from a python script example of what an icon looks like when added to the application and viewed through Windows Explorer:. The following file taken from the PyInstaller test suite how can i create a stand-alone binary from a python script used by PyInstaller to add version information to the executable. Save this file as version. When you have your version.
Of course, I glossed over some of the complicated bits like runtime libraries, 3rd party modules, spec files, and program data files. The PyInstaller Manual covers all of this, so give it a read. Creating an Executable from a Python Script Python is one of my favorite programming languages. First Things First Of course, make sure that you already have Python 2. You will need to install PyInstaller as well, but I will get to that in a second.
The App This app will be a simple "Hello World" graphical app. Now, fire up your console and run the app as usual. It's not very exciting, but this is just a demo. Let me briefly describe the options that are being used: If you do not specify this option, the libraries, etc. If you're releasing a non-graphical application i. The basename of this script will be used to name of the executable, however you may specify an alternative executable name using the --name option.
Adding an Icon IconEden offers some nice royalty-free icons, so I'll use one of theirs for this demo. Adding Version Information The following file taken from the PyInstaller test suite is used by PyInstaller to add version information to the executable. How can i create a stand-alone binary from a python script at the Properties to see more information.
Final Thoughts So that's about it. If PyInstaller isn't what you're looking for, here are some alternatives: