Creating a Single Executable File (.exe) from a Python Program

How to make a single executable file from a python program ?

Mounir Boulwafa
4 min readJan 27, 2019
Creating a Single Executable File (.exe) from a Python Program
Creating a Single Executable File (.exe) from a Python Program

I’ve just started programming using python, and I made my first application as a single executable file that can be installed and runs in any other computer even if it doesn’t have python installed.

So, my idea here is to show you the tools that I’ve used and explain step by step how I’ve created the .exe file of my python program.

Tools :

  1. PyInstaller 3.4
  2. NSIS (Nullsoft Scriptable Install System) 3.04

I’m using for this example a simple graphical application that i made using Python 3.7.2 (64-bit) on my Windows 10 (64-bit) laptop.

The application has one image and a simple button that shows a messagebox saying “Hello Medium”, as you can see in the image below:

Hello Medium Python Application

This python program we are using in this example is very simple, but here is the source code in my github repo if ever you wanna check it :

Step 1 : Building the Executable File

In this step we are going to use PyInstaller to create the exe file.

Make sure you have PyInstaller installed, if this not the case, you just have to run the following command:

pip install pyinstaller

Note: You might need to install PyWin32 as well.

Note 2: If you have problems installing PyInstaller, just check this link it may help you : https://stackoverflow.com/questions/44740792/pyinstaller-no-module-named-pyinstaller/54338665#54338665

Now, for Building the executable file, first make sure that your program is working well, then run this command:

pyinstaller -–onefile -–windowed --icon="images\myicon.ico" myApp.py
  • --onefile / -F : Create a one-file bundled executable.
  • --windowed / --noconsole / -w : do not provide a console window when the application is run. If your application is a console application, you do not need to use this option.
  • --icon / -i : set an icon to the executable file.

You can use also --name, --version, etc…, see all the options here:

The command will create a /dist directory in your program folder that contains the standalone executable file.

Now, all you have to do is to run your executable file.

Note : if your program is depending on additional files (in my example: it depends on images folder), You need to copy these files into the same directory of your executable file.

Step 2 : Creating the installer file

In the previous step we created the executable file from our python code, but we still have multiple files (the exe file + the dependencies).

So, in this step, we’re going to make a setup executable file “setup.exe” that will be used to deploy your application and let other people install it.

We are going to use here : NSIS (Nullsoft Scriptable Install System).

First, create a ZIP file of all the necessary files (the executable file, dependencies, etc.).

Then, Run NSIS, and chose: Installer based on .ZIP file.

  • Select the ZIP file.
  • Provide a name.
  • Set the installation directory (e.g. Program Files\myApp).
  • Generate the setup.exe file.

Once the processing is finished, you’ll have the setup.exe file of your application which is easily downloadable and runnable by others.

This is my experience, sharing to Medium writers. Make sure to follow me on : Medium, Linkedin, Twitter, Instagram to get more updates.

And also if you liked this article make sure to share it with your friends.

Thanks for your reading.

--

--

Mounir Boulwafa

Full-Stack Data Scientist (w software engineering background). Machine Learning/AI. NLP. 🇲🇦 — twitter.com/mounirboulwafa | linkedin.com/in/mounirboulwafa