I have a confession to make. I use Windows for coding Python.
This means that I often need to run my practical coding projects as Windows
.exe files, especially if I work with non-technical clients that don’t know how to run a Python file.
In this tutorial, I’ll share my learnings on making a Python file executable and converting them to an
.exe so that they can be run by double-click.
To make a Python script executable as a
.exe file on Windows, use a tool like
pyinstaller. PyInstaller runs on Windows 8 and newer.
⭐ Pyinstaller is a popular package that bundles a Python application and its dependencies into a single package, including an
.exe file that can be run on Windows without requiring a Python installation.
Here are the general steps to create an executable file from your Python script using Pyinstaller:
- Install Pyinstaller by opening a command prompt and running the command:
pip install pyinstalleror
pip3 install pyinstallerdepending on your Python version.
- Navigate to the directory where your Python script is located in the command prompt using
cd(command line) or
- Run the command:
pyinstaller --onefile your_script_name.py. This command creates a single executable file of your Python script with all its dependencies included.
- After the command completes, you can find the executable file in a subdirectory called
- You can now distribute the executable file to users, who can run it on their Windows machines by double-clicking the
What Does the –onefile Option Mean?
--onefile file specifier is an option for Pyinstaller that tells it to package your Python script and all its dependencies into a single executable file.
By default, Pyinstaller will create a directory called
dist that contains your script and a set of related files that it needs to run. However, using the
--onefile option, Pyinstaller will generate a single
.exe file, which is more convenient for the distribution and deployment of the application.
To convert a Python file
my_script.py to an executable
my_script.exe using Pyinstaller, install Pyinstaller using
pip install pyinstaller, navigate to the script directory in the command prompt, run
pyinstaller --onefile my_script.py, then locate the executable file in the
If you want to keep improving your coding skills, check out our free Python cheat sheets!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.