Fundamentals 6 min read

How to Turn Any Python Script into a Standalone EXE with PyInstaller

Learn step‑by‑step how to install PyInstaller, understand its packaging mechanism, and use command‑line options to bundle Python scripts—including those with third‑party libraries or PyQt5 GUIs—into standalone Windows executables, complete with custom icons and without console windows.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Turn Any Python Script into a Standalone EXE with PyInstaller

Introduction

This guide shows Python enthusiasts how to package their scripts into executable files that run on Windows without requiring a Python interpreter.

Installing PyInstaller

First, install PyInstaller using the command: pip3 install pyinstaller After installation, you will see a confirmation similar to the screenshot below.

How PyInstaller Packages

Python scripts cannot run independently of the interpreter, so PyInstaller bundles the interpreter, the script, and all required third‑party packages into a single .exe. Even if your project only uses requests, PyInstaller includes all its dependencies.

Creating a Simple EXE

Navigate to the directory containing your script and run: pyinstaller -F setup.py The

-F</p> option forces a fresh build each time. After execution, PyInstaller creates several folders; the <code>dist

folder contains the generated .exe.

Run the setup.exe located beside setup.py to verify it works without a Python environment.

Packaging a GUI Application

When packaging a PyQt5 GUI, add the -w flag to suppress the console window: pyinstaller -F -w setup.py To set a custom icon, include the -i option with an .ico file:

pyinstaller -F -w -i wind.ico setup.py

After packaging, the executable displays the specified icon.

Common Commands Summary

pyinstaller -F setup.py

– package script into a single exe. pyinstaller -F -w setup.py – package without a console window. pyinstaller -F -w -i xx.ico setup.py – package with a custom icon.

Conclusion

After upgrading pip to the latest version, install PyInstaller and follow the steps above to create portable executables for any Python script, even those with GUI components. This eliminates the need for a Python environment on the target machine.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonpackagingpyinstallerexe
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.