avatarThe PyCoach

Summary

This article provides two methods for converting a Python script into an executable file (.exe): using auto-py-to-exe and PyInstaller.

Abstract

The article discusses the benefits of converting Python scripts into executable files, such as hiding code or scheduling jobs. It then presents two methods for converting scripts: auto-py-to-exe, which offers a graphical user interface, and PyInstaller, which is a command-line tool. The article provides step-by-step instructions for using both methods, including installing the necessary libraries, setting up the conversion process, and running the executable file. It also highlights the differences between the two methods, such as the ability to create a single directory or file and the option to display the console.

Bullet points

  • Two methods for converting Python scripts into executable files: auto-py-to-exe and PyInstaller
  • auto-py-to-exe offers a graphical user interface for easy conversion
  • PyInstaller is a command-line tool that requires more technical knowledge
  • Both methods allow for creating a single directory or file and displaying the console
  • The article provides step-by-step instructions for using both methods
  • The conversion process requires installing the necessary libraries and setting up the conversion process
  • Running the executable file can be done by double-clicking or scheduling a job
  • Converting scripts into executable files can be useful for hiding code or scheduling jobs.

How to Easily Convert a Python Script to an Executable File (.exe)

Two simple ways to create a Python executable file.

Image by author

Although running a Python script using the terminal or your favorite text editor is straightforward, there are some situations in which you will prefer to hide all the code written in the script (.py) inside an executable file (.exe).

Maybe you need to send the script to someone who doesn’t code at all or you might need to schedule a job that runs a .exe at a specific time on your computer. Whatever the situation, in this guide, I will show you 2 ways to create an executable file. The first (auto-py-to-exe) has a friendly interface that will help beginners to easily create executables, while the second (PyInstaller) offers a straightforward way to create executables through the terminal.

Table of Contents
1. Making an Executable file with auto-py-to-exe
 - Installing with pip
 - Running auto-py-to-exe
 - Step 1: Add the script location
 - Step 2: Choosing “One Directory” or “One File”
 - Step 3. Choosing “Console Based” or “Window Based”
 - Step 4: Advanced option(e.g. output directory, additional import)
 - Step 5: Convert the file
2. Making an Executable file with PyInstaller

Making an Executable file with auto-py-to-exe

The first option offers a nice GUI (graphical user interface) that takes care of all the stuff necessary to convert your Python script into an executable file.

By the way, you can also watch my YouTube video to learn how to convert a .py to .exe, in case you prefer watching the steps below rather than reading them.

Installing with pip

To install the last version of auto-py-to-exe, just open up a terminal and run the following command.

pip install auto-py-to-exe

Note: Make sure that the working environment in which you’re installing auto-py-to-exe contains all the libraries that your script needs to run.

Running auto-py-to-exe

Once you install auto-py-to-exe, making an executable file is as easy as writing the following command.

auto-py-to-exe

After running the command, the following GUI application will open.

Image by author

I will walk you through each option to properly create an executable file.

Steps to create an executable file

Step 1: Add the script location

Browse the script you wish to convert and add it to the “Script Location” field. In this example, I’m going to choose a script that automates Excel reports (you can find my guide to automate Excel in the link below)

Feel free to choose any script you want. However, if your script needs to read a path make sure that you use absolute paths since relative paths won’t behave as you might expect with executable files. If necessary, include the following line of code below to know where the executable file is located and make the necessary changes to your script so you read/export files on the right directory.

application_path = os.path.dirname(sys.executable)

Step 2: Choosing “One Directory” or “One File”

Now we have to choose whether we want to create “one directory” or “one file.” The first creates a directory with all the dependencies your script needs to run (including the executable file), while the second creates only a single executable file.

For this example, I’ll choose the “one file” option.

Step 3. Choosing “Console Based” or “Window Based”

Now it’s time to choose whether the console will be displayed or not. If you choose “Console Based,” the console will open after running the executable file, which is recommended if your script generates console-based outputs. However, if you don’t want to show the console outputs when running the executable file, choose “Window Based”

My script needs the name of the Excel spreadsheet to be introduced as input in order to create my Excel report, so I’m going to choose “Console Based.”

Step 4: Advanced options (e.g., output directory, additional imports)

You can add an icon, add files that your script needs to run, and more! However, for this example, I’ll only modify the path where the executable file will be exported. To do so, click on the “Setting” option and browse the output directory you wish.

Image by author

Note: If you see an error like this “ModuleFoundNotError: Not module named ‘ name_of_module’” after double-clicking on the executable file created, you’ll have to repeat from step 1 again, but now in the “Advanced” option write the module name is missing inside the “hidden-import” field.

Step 5: Convert the file

To convert the .py file to .exe just click the blue button you see below.

Image by author

Something really important that auto-py-to-exe shows above the convert button is the code that pyinstaller (the main library and second option in this guide to make .exe files) needs to create an executable file behind that fancy GUI you see on the screen.

Once the process is finished the executable file should be located in the output directory you set in step 4!

Making an Executable file with PyInstaller

This option fits better for those who prefer to quickly create an executable file running a command on the terminal.

If you’re used to working with the terminal, then the PyInstaller library will be the best option. To install PyInstaller follow these steps.

  • Step 1: Open up a terminal and run pip install pyinstaller
  • Step 2: Using the terminal, go to the directory where your script is located (use the cd command)
  • Step 3: Once you‘re in the right directory, write a command with the following syntaxpyinstaller --onefile name_of_script.py in the terminal to make the script executable.

The command used in step 3 is similar to the code shown in the step 5 picture for the auto-py-to-exe option. You can play a little bit with the GUI offered by auto-py-to-exe to get used to the many options you can add to this command.

After running the command, you should see a message that says “completed successfully.” In the directory where your script is located, a folder named “dist” should have been created. Inside the folder, you’ll find the standalone executable!

Congratulations! Now your Python script has been converted to an executable file. In case you want to schedule when this file will run on your computer check this guide.

Automate your life with Python! Get my FREE Automation Cheat Sheet by joining my email list with 20k+ people.

Python
Data Science
Technology
Programming
Productivity
Recommended from ReadMedium