How to Use Pyinstaller to Generate an EXE File

Luca Liu - Jan 10 - - Dev Community

As a Data Analyst, one common challenge I face is trying to share a python script for data processing with colleagues who may not have an IT background or are not familiar with Python. Oftentimes, their computers do not have Python environment.

Pyinstaller is an incredibly useful tool for packaging your Python code into a standalone executable file. This makes it simple to distribute and run your code on various systems without the need for users to have Python installed. In this blog post, we'll guide you through the process of using Pyinstaller to create an EXE file from your Python script.

Packaging Python Code with PyInstaller

Step 1: Install pyinstaller

Make sure Pyinstaller is installed on your system using pip. Run the following command in your terminal or command prompt to install it.

pip install pyinstaller
Enter fullscreen mode Exit fullscreen mode

Step 2: Navigate to Your Python Script

Navigate to the directory where your Python script is located using the terminal or command prompt.

Step 3: Run Pyinstaller

To generate an EXE file from your Python script, you can use the following command:

pyinstaller -F python_script.py
Enter fullscreen mode Exit fullscreen mode

In this command, -F specifies that you want a executable file, which includes all third-party dependencies, resources, and code. Replace python_script.py with the name of your Python script.

Step 4: Locate the Generated EXE File

After running the Pyinstaller command, you will find that three folders are generated: pycache, build, and dist. The dist folder contains the executable file of your Python script. You can now distribute this EXE file to others, and they can run your script without needing to install Python!

Exploring further more options

There are numerous options available for customization when using pyinstaller. Some commonly used examples include:

  1. -n, --name: Allows you to specify the name of the executable file.

    pyinstaller --name myprogram myscript.py or pyinstaller -n myprogram myscript.py
    
  2. -w, --windowed: Causes the console window to be hidden when the program is run.

    pyinstaller --windowed myscript.py or pyinstaller -w myscript.py
    
  3. -y, -noconfirm: Replace output directory (default: SPECPATH/dist/SPECNAME) without asking for confirmation.

  4. -additional-hooks-dir: Specifies a directory to search for additional hooks.

Packaging Python Code with GUI

If you find the options for packaging Python scripts to be complex and are seeking a more straightforward solution, Auto-py-to-exe is an excellent tool to consider. It offers a user-friendly graphical interface for converting Python scripts into standalone executable files using Pyinstaller. This tool streamlines the process by guiding users through the necessary configurations and options, making it easier to create executable files from Python scripts.

Here's how you can use it:

Installation:

First, ensure that you have auto-py-to-exe installed.

pip install auto-py-to-exe
Enter fullscreen mode Exit fullscreen mode

Usage:

Once auto-py-to-exe is installed, you can run the tool by executing the following command in your terminal or command prompt:

auto-py-to-exe
Enter fullscreen mode Exit fullscreen mode

This command will open a graphical user interface (GUI) for auto-py-to-exe, allowing you to proceed with the conversion process.

Image description

Customization:

The tool offers various customization options, including the ability to set the icon for the executable file and choose whether the console window should be shown during execution. These options are presented within the GUI, making it easy to configure the desired settings for the executable file.


Explore more

Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.

🚀 Connect with me on LinkedIn

🎃 Connect with me on X

🌍 Connect with me on Instagram

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .