Skip to Content

How to convert Python script to exe

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:

  1. Python 3.6 or above
  2. pip (Python’s package manager)
  3. 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:

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

if __name__ == "__main__":
    print("Simple Calculator")
    x = int(input("Enter first number: "))
    y = int(input("Enter second number: "))

    print("1. Add")
    print("2. Subtract")

    choice = input("Choose an operation (1/2): ")

    if choice == "1":
        print("Result:", add(x, y))
    elif choice == "2":
        print("Result:", subtract(x, y))
    else:
        print("Invalid input")

        

After converting it to .exe, you can share this file with anyone — they don’t even need Python installed.

 Optional Flags in PyInstaller

FlagPurpose
--onefileCombine everything into a single executable
--noconfirmReplace output folder without asking
--icon=icon.icoAdd a custom icon to your .exe
--noconsoleSuppress terminal window for GUI apps

Example with icon:

pyinstaller --onefile --icon=myicon.ico calculator.py


Hope you find it helpful!!!

All Major Protocols in Computer Science Explained