How to Use Pyinstaller to Generate an EXE File
Tags: python, dataanalysis, dataengineering, programming
Published: 2024-01-10 13:47:14

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.

```python
pip install pyinstaller
```

### 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:

```python
pyinstaller -F python_script.py
```

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.
    
    ```python
    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.
    
    ```python
    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.

```python
pip install auto-py-to-exe
```

### Usage:

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

```python
auto-py-to-exe
```

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4g9we961uh9bzhi9uj90.png)

### 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

{% embed https://dev.to/luca1iu %}


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

{% cta https://www.linkedin.com/in/lucaliu-data %} 🚀 Connect with me on LinkedIn {% endcta %}

{% cta https://twitter.com/Luca_DataTeam %} 🎃 Connect with me on X {% endcta %}


{% cta https://www.instagram.com/luca1iu?igsh=MmJneDFpdjNtYWU1&utm_source=qr %} 🌍 Connect with me on Instagram {% endcta %}