In many cases, especially in real-world software development or when sharing your Python application with someone who doesn't have Python installed, converting your Python script to a .exe file can be very useful. It allows others to run your program just like any other Windows application — with a simple double-click.
In this blog, we’ll walk you through how to convert your Python file (.py) into a Windows executable (.exe) using the most commonly used tool — PyInstaller.
Why Convert Python to .exe?
- You want to share your project with clients, friends, or teammates who don't have Python installed.
- You’re developing a desktop application and need to distribute it.
- You want to protect your source code from being easily editable.
Prerequisites
Before converting Python to .exe, make sure the following are installed:
- Python 3.6 or above
- pip (Python’s package manager)
- PyInstaller (you’ll install it shortly)
To check your Python version: python --version
Step-by-Step: Convert .py to .exe Using PyInstaller
Let’s say you have a file called calculator.py.
Step 1: Install PyInstaller
First, open the terminal or command prompt and install PyInstaller:
pip install pyinstaller
Step 2: Go to Your Script’s Folder
Navigate to the folder where your Python script is located:
cd path\to\your\script
Example:
cd C:\Users\yourname\Documents\python-apps\
Step 3: Convert to Executable
Run the following command: pyinstaller --onefile calculator.py
Explanation:
-
--onefile bundles everything into a single.exe file. -
You can also add
--noconsole if it's a GUI app and you don't want a terminal to open.
Example for GUI app:
pyinstaller --onefile --noconsole calculator.py
Step 4: Locate the .exe File
After running the command, you’ll see some new folders created:
- build/
- dist/
- calculator.spec
Your final .exe will be inside the dist/ folder:
dist/
└── calculator.exe
Now, you can double-click and run the file. It will behave just like a normal Windows program.
Practical Example: Simple Calculator App
Here’s a simple calculator.py script:
After converting it to .exe, you can share this file with anyone — they don’t even need Python installed.
Optional Flags in PyInstaller
| Flag | Purpose |
|---|---|
| --onefile | Combine everything into a single executable |
| --noconfirm | Replace output folder without asking |
| --icon=icon.ico | Add a custom icon to your .exe |
| --noconsole | Suppress terminal window for GUI apps |
Example with icon:
pyinstaller --onefile --icon=myicon.ico calculator.py
Hope you find it helpful!!!