Methods of payment Abuse

How to Turn a Python Script into an Executable File

11.06.2025, 14:41

When you write a script in Python, everything runs smoothly — because you have Python and all the necessary libraries installed. But if you want to run that script somewhere else (like sending it to a friend or running it on your work laptop), you'll run into a maze of installations and dependencies. Luckily, there's a way to bundle everything into one standalone file that works like a regular program. Here's how to do it.

Step 1: Install PyInstaller

Open your terminal (or Command Prompt, or the built-in terminal in VS Code) and run:

pip install pyinstaller

Don’t close the terminal — we’ll need it again soon.

Step 2: Navigate to Your Script's Folder

Next, move into the folder where your Python script is located. Use the cd command followed by the path to the folder. For example:

cd /Users/username/Downloads

If everything’s done right, the terminal will now show the folder name — that means you’re inside it.

How the Compiled File Works

PyInstaller can generate either:
— A folder with all necessary files, or
— A single .exe file (or .app on macOS).

Functionally, both do the same thing. When you run the generated file, it:
1. Creates a temporary "virtual folder";
2. Unpacks all required libraries and files into it;
3. Starts Python in that environment;
4. Then runs your script inside it.

So what you get is a compact package of everything your script needs to run — no manual setup required.

Step 3: Build the Executable

Now for the main part. Since we want a single file (not a folder), we’ll use the --onefile flag. The command looks like this:

pyinstaller --onefile your_script.py

For example:

pyinstaller --onefile gui.py

After that, two new folders will appear in the same directory:
— build — you can ignore this one;
— dist — this is where your .exe or .app file will be.

If you’re on Windows, you’ll get a Windows executable. If you’re on Mac, you’ll get a Mac app.

The final file might be 20–30 MB in size. That’s totally normal — it includes your code plus all the necessary libraries to run it.

Step 4: Test the Result

Double-click the generated file. A terminal window should open, and shortly after — your program’s interface (if it has one).
If everything works, congrats — you’ve done it right! You can now run this file on any computer, even if Python isn't installed. No setup needed — just run and go.

If you want, PyInstaller also allows for more advanced settings: removing the console window, changing the app icon, or customizing app behavior. But that’s for later. For now, you know how to get started.