How to Create an executable from a Python program

Applications or scripts developed with the Python language can be converted into executables for the Windows operating system. In this way, they can be usedwithout having to install Python and are thus made available to as many people as possible. It is possible to perform this conversion with different modules created for this purpose. To use one of these modules, you must of course have previously installed Python on your machine.

Read: How To Check Python Version

In this article, we shall consider a fairly simple Python project for calculating the body mass index or BMI . The program receives as input the user’s height and weight and prints out the BMI according to the formula Weight / Height :

weight = float(input(‘Enter your weight in kg: ‘))

height = float(input(‘Enter your height in meters: ‘))

bmi = weight / (height * height)

print(‘Your BMI is :’+ str(bmi))

The program works well, we can even test it out:

Read: How to use Yield in Python

How to deliver the program as an executable ?

Now that the program is up and running, it is time to send it over to some friends and colleagues. But not everyone has python installed, and it won’t make sense to ask them to install a Python code editor or IDE, so how to run a Python program without its compiler ?

Taking a look at your computer, you will find several programs installed that can run files without the need to install any interpreter or IDE. How to make Python script executable ?

Read: How to run Python on Android

Python to exe transformation

The ideal would be to transform our file into a generic executable (like .exe files in Windows), that could run without problems.

Generating an exe file with cx_Freeze

There are already several tools and scripts that can do this conversion process like PyInstaller , py2exe and cx_Freeze .

In the first section below, we will use cx_Freeze which is a set of scripts and modules used to convert an application developed in Python into an executable. It has the advantage of running on Windows, Mac or Linux. To install it, you can use the package manager supplied directly with Python, PIP.

Read: 6 Reasons why you Should Learn Python

The first step, as always, is to install this Python package. For this, we use the pip command. In the terminal, type in the following :

pip install cx_Freeze

Basic solution

Once we installed the package, a simple command in the terminal is enough to create our executable. The base utility is simply cx_freeze, but, as we want to organize it a little better, we will use the flag –target-dir to indicate the folder where we want our files to be located (visit the official page here for more on the command):

cxfreeze –target-dir calculator-bmi

The folder calculator-bmi is created. It will contain two library files, lib and which contain the necessary data for the executable to work. The file bmi (in Windows it would be called bmi.exe) is the executable.

A more complete solution

If the previous solution did not work, you can try out the following more complex alternative.

Go to your program directory. Create the “” script. It should contain the code shown below. Replace the name “” with the name of the script launching your application, in our case.

Read: How to run Python on Ubuntu 20.04

You must also modify the variables “name”, “version” and “description” in the last command to adapt them to your own.

from cx_Freeze import setup, Executable

// Replace “” by the name of the script which starts your program

executables = [Executable (“”)]

// Adapt the values ​​of the “name”, “version”, “description” variables to your program.

setup ( name = “My Program”,
options = options,
version = “1.0”,
description = ‘This is my program’,
executables = executables )

In your command prompt in your program directory , run the following command to start generating the executable :

python build

The executable is then located in the “build” folder created in your program folder. For more information, refer to the cx_Freeze github page.

Read: How to get the timestamp in Python

Using pyinstaller

In this section, we will use the program pyinstaller. Follow the steps below to create an executable of your Python program.

Install the PyInstaller package by running the command below on your terminal:

pip install pyinstaller

Once the installation is done correctly, access your project folder. Now invoke the command below to generate the executable (make sure to replace with your program name):


Two folders (build and dist) and a configuration file with the extension .specs will be generated. This file can be modified to generate different results, see
here . What matters to us is what is in the dist folder.
Go ahead and run the generated .exe file.

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.


Marianne elanotta

Marianne is a graduate in communication technologies and enjoys sharing the latest technological advances across various fields. Her programming skills include Java OO and Javascript, and she prefers working on open-source operating systems. In her free time, she enjoys playing chess and computer games with her two children.

Leave a Reply