How To Compile Python In Terminal

In this tutorial, we will learn how to compile Python code directly from the terminal. Compiling Python code typically refers to creating a standalone executable binary from your Python script.

This process allows users to run a Python script without needing to have Python installed on their system. This is very useful for distributing your applications to other users.

We’ll use a Python package called PyInstaller which is one of the most popular Python packages for compiling Python scripts. We will look at how to install PyInstaller, how to compile a basic script, and how to compile a script with external dependencies or packages.

Step 1: Install PyInstaller

To begin, open your terminal (macOS or Linux) or command prompt (Windows). The first thing you need to do is install PyInstaller. This can be done using the Python package manager, pip. Run the following command to install PyInstaller:

Now that you have PyInstaller installed, you can use it to compile Python scripts.

Step 2: Create a Python Script

For this tutorial, let’s create a simple Python script called “” with the following content:

This script simply prints “Hello, World!” when executed.

Step 3: Compile Your Python Script

To compile your Python script with PyInstaller, navigate to the directory in which your script is located using the terminal, and then run the following command:

The --onefile flag is used to create a single executable file. This command will generate the compiled executable binary file in the ‘dist’ folder in the same directory.

The output file will be named ‘hello_world.exe’ on Windows systems or just ‘hello_world’ on macOS and Linux systems.

To execute the generated binary, navigate to the ‘dist’ folder and run:

On Windows systems, you can double-click the ‘hello_world.exe’ file to execute it.

The output should be:

Hello, World!

Step 4: Compiling Scripts with External Dependencies

If your Python script uses external libraries or packages, you must install them in the same environment where PyInstaller is installed. For example, let’s modify our “” script to use the popular requests package:

First, install the requests package using pip:

Now you can use the same PyInstaller command to compile the script:

The resulting executable binary will now have the required dependencies bundled with it.

Step 5: Customize the Compiled Binary

You can customize the name of the output file or the icon (on Windows) using flags in the PyInstaller command. For example, to change the output file name to “my_app” and use a custom icon called “app_icon.ico”:

Please refer to the official PyInstaller documentation for more advanced options and configurations.

Full Code


This tutorial taught us how to compile a Python script using the terminal and the PyInstaller package.

Compiling your Python scripts into executable binaries simplifies distribution to users who may not have Python installed on their systems. In addition, it reduces the complexity of script execution for non-technical users, as they can simply run the generated executable binary without needing to interact with the Python interpreter.