Build Python script to Executable file for Windows/MacOS/Linux

This tutorial will show you how to convert Python to exe using PyInstaller.

Make sure you have Python installed #

The first thing is, of course, you have to install Python. Please don't forget to add Python to your PATH environment.

Install Pyinstaller #

Open your command promt/terminal and execute the command to install PyInstaller

pip install Pyinstaller

Build Python script to executable binary file #

Let say we have a very simple Python script that just print the Hello world text to the console. This file name is "test.py"

test.py:


print("Hello world")

To build that Python to exe we can use Pyinstaller which was installed in previous step

pyinstaller --onefile test.py

By running above command, PyInstaller will build our Python script to executable file: test.exe. Depend on where you run that command (Windows, MacOS or Linux), the executable file will be runnable for corresponding platform. It mean, if you are using Windows and running that command, it can be run in Windows but not in MacOS or Linux. Similar, if you run that command in MacOS, it can be run in Mac but not in Windows/Linux.

Add icon to the exe file #

Pyinstaller allows us adding the icon to the executable file. To do so, just add the --icon options

For example:

pyinstaller --onefile --icon=path_to_icon.ico test.py

If you are building the GUI app and want to build it to executable file. You can do the same command as above. However, besides the GUI window, it also displays the console window. To eliminate the console window, we can use the --windowed option.

For example:

pyinstaller --onefile --windowed --icon=path_to_icon.ico test.py

Tags:

Python