Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python to .exe – How to Make a Python Script Executable?
1
Python to .exe – How to Make a Python Script Executable?

5/5 – (1 vote)

I have a confession to make. I use Windows for coding Python.

[Image: image.gif]

This means that I often need to run my practical coding projects as Windows
Code:
.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
Code:
.exe
so that they can be run by double-click.

PyInstaller

[Image: image-140.png]

To make a Python script executable as a
Code:
.exe
file on Windows, use a tool like
Code:
pyinstaller
. PyInstaller runs on Windows 8 and newer.

[Image: 2b50.png] Pyinstaller is a popular package that bundles a Python application and its dependencies into a single package, including an
Code:
.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:

  1. Install Pyinstaller by opening a command prompt and running the command:
    Code:
    pip install pyinstaller
    or
    Code:
    pip3 install pyinstaller
    depending on your Python version.

  2. Navigate to the directory where your Python script is located in the command prompt using
    Code:
    cd
    (command line) or
    Code:
    ls
    (PowerShell).

  3. Run the command:
    Code:
    pyinstaller --onefile your_script_name.py
    . This command creates a single executable file of your Python script with all its dependencies included.

  4. After the command completes, you can find the executable file in a subdirectory called
    Code:
    dist
    .

  5. You can now distribute the executable file to users, who can run it on their Windows machines by double-clicking the
    Code:
    .exe
    file.

What Does the –onefile Option Mean?

The
Code:
--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
Code:
dist
that contains your script and a set of related files that it needs to run. However, using the
Code:
--onefile
option, Pyinstaller will generate a single
Code:
.exe
file, which is more convenient for the distribution and deployment of the application.

1-Paragraph Summary

To convert a Python file
Code:
my_script.py
to an executable
Code:
my_script.exe
using Pyinstaller, install Pyinstaller using
Code:
pip install pyinstaller
, navigate to the script directory in the command prompt, run
Code:
pyinstaller --onefile my_script.py
, then locate the executable file in the
Code:
dist
folder.

If you want to keep improving your coding skills, check out our free Python cheat sheets!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: