How to Convert PY to EXE in Python

In this Python article we want to learn How to Convert PY to EXE in Python, so Python is popular programming language used for developing different applications from web development to machine learning and data analysis. while Python code can be run directly from the terminal or an integrated development environment (IDE), sometimes it is necessary to convert Python script into an executable file. this can be useful for distributing your code to users who don’t have Python installed or to make it easier to run your code on different operating systems. in this article we want to go over the steps for converting Python script (PY) into an executable file (EXE) using two popular libraries PyInstaller and cx_Freeze.

 

 

 

How to Convert PY to EXE in Python

So first of all let’s talk about PyInstaller, PyInstaller is popular library for converting Python scripts into standalone executables. it works by analyzing your Python code and packaging it into single executable file that includes everything your code needs to run including Python interpreter, any dependencies and any other resources like images or data files, we need to install this library and we can use pip for the installation.

 

 

So now let’s create simple Python code that we want to convert that to EXE, we can call this simple.py

 

 

for converting the simple.py script into an executable file open your terminal or command prompt and navigate to the directory where the script is located. after that enter the following command:

This will creates new directory called dist that contains the executable file. you can distribute this file to users who don’t have Python installed on their machines.

How to Convert PY to EXE in Python
How to Convert PY to EXE in Python

 

 

Another library is cx_Freeze , cx_Freeze is another tool for converting Python scripts into executable files. it works by creating zip file containing your Python code, along with any dependencies and resources, and then compiling it into an executable file.

 

 

Create new Python script called setup.py in the same directory as your script that you want to convert into an executable. this script will tell cx_Freeze how to package your code.

 

 

After that run this command for building exe file

This will create new directory called build that contains the executable file like this.

PY to EXE
PY to EXE

 

 

Learn More on Python GUI

Leave a Comment