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.
PyInstaller
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 pyinstaller
orpip3 install pyinstaller
depending on your Python version. - Navigate to the directory where your Python script is located in the command prompt using
cd
(command line) orls
(PowerShell). - 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
dist
. - You can now distribute the executable file to users, who can run it on their Windows machines by double-clicking the
.exe
file.
What Does the –onefile Option Mean?
The --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.
1-Paragraph Summary
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 dist
folder.
If you want to keep improving your coding skills, check out our free Python cheat sheets!