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.
1 |
pip install pyinstaller |
So now let’s create simple Python code that we want to convert that to EXE, we can call this simple.py
1 |
print("Welcome to GeeksCoders.com") |
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:
1 |
pyinstaller –w simple.py |
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.
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.
1 |
pip install cx_Freeze |
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.
1 2 3 4 5 6 7 8 |
from cx_Freeze import setup, Executable setup( name="simple", version="0.1", description="My geekscoders app", executables=[Executable("simple.py")] ) |
After that run this command for building exe file
1 |
python setup.py build |
This will create new directory called build that contains the executable file like this.
Learn More on Python GUI
- How to Create Print Dialog in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6