DEV Community

Cover image for Easy way to convert Python Script to Exe
Paul
Paul

Posted on

Easy way to convert Python Script to Exe

You've written a Python script tested it on your computer, and now want to share it with someone who doesn't have Python installed.

The easiest way to do that is to convert your Python script into an executable (.exe) file. This allows other Windows/ Mac users to run your program just like any other application.

There are a few ways to do this. Lets look at 2 ways to do this, we'll first go with the traditional way then look at easy drag and drop option

1. Using PyInstaller

PyInstaller is probably the most popular option for converting Python scripts into executable files.

First, install it using pip:

pip install pyinstaller
Enter fullscreen mode Exit fullscreen mode

Now, navigate to the folder containing your Python script and run:

pyinstaller --onefile main.py
Enter fullscreen mode Exit fullscreen mode

Replace main.py with the name of your Python file.

Once the process finishes, open the dist folder. You'll find your .exe file there.

Want to add an application icon?

You can use the --icon option:

pyinstaller --onefile --icon=app.ico main.py
Enter fullscreen mode Exit fullscreen mode

A few useful options:

--onefile: Packages everything into a single executable.

--windowed: Prevents the console window from appearing (useful for GUI applications).

--name: Lets you specify the executable's name.

--icon: Adds a custom application icon.

For example:

pyinstaller --onefile --windowed --icon=app.ico --name MyApp main.py
Enter fullscreen mode Exit fullscreen mode

One thing to remember: PyInstaller generally needs to build the executable on the target operating system. If you want a Windows EXE, you'll normally need to run it on Windows.

2. Using py2exe Conveter

Py2exe converter

If you don't want to install packaging libraries or deal with command-line configuration, there's another option.

You can use Py2Exe by PyUiBuilder.

It lets you upload your Python script, configure the application name and icon, and build an executable through a browser-based interface. It can also produce both Mac and windows apps independent of OS

You don't need to manually run PyInstaller commands or configure a packaging environment yourself.

If you want to self-host it, it's open source.

Which one should you use?

It really depends on what you're building.

PyInstaller: A great starting point for most Python projects.

P2exe PyUiBuilder: A convenient option if you'd rather package your scripts through a graphical interface and want to distribute for both mac and windows

Top comments (0)