Bundle a Python Application to an EXE with PyInstaller: A Beginner Guide – Python Tutorial

By | December 9, 2019

Python pyinstaller can help us to bundle a python application to exe file, it is easy to use. In this tutorial, we will introduce how to use pyinstaller to bundle a python application. As a python beginner, you can learn how to do from this tutorial.

Install pyinstaller

We should install python pyinstaller first.

pip install pyinstaller

we can use pip command to install this package.

python install pyinstaller

Bundle a python application with pyinstaller

In this tutorial, we will use an example to show how to bundle a python application using pyinstaller.

First, we navigate to the directory where the python files are located, for example: E:\workspace-nlp\PNG2EPS

Then, we can use command below to bundle a python application

(py3.5) E:\workspace-nlp\PNG2EPS>pyinstaller -i png2eps.ico -F -n PNG2EPS main.py

To bundle a python application, the basic usage is:

pyinstaller -parameters file_name.py

As to code above:

-parameters: -i png2eps.ico -F -n PNG2EPS

file_name.py: main.py

pyinstaller build python to exe sucessfully

To use pyinstaller correctly, we should remmember some basic pyinstaller parameters.

pyinstaller parameters

Here we will introduce some pyinstaller parameters we often use.

parameter definition
-i file.ico, determines the ico of exe application
-F means pyinstaller will only create a one-file bundled executable
-n the name of exe file
-c means the exe file will open a console window for standard i/o, only works on windows and mac system

As to example above, we will generate a PNG2EPS.exe in dist folder.

Moreover, you may get error: PyInstaller cannot check for assembly dependencies after having installed pywin32-ctypes. In order to fix this error, you can view this tutorial.

Fix PyInstaller cannot Check for Assembly Dependencies After Having Installed pywin32-ctypes