DEV Community

Cover image for The Easiest Way to Package Your Python Files(Turn to .exe Files)
SomeB1oody
SomeB1oody

Posted on

The Easiest Way to Package Your Python Files(Turn to .exe Files)

1. Introduction

Python is an interpreted language, so it does not produce executable files (e.g., .exe) during compilation. However, many programs are based on Python. To simplify the packaging process, I wrote a program to automate it (with one prerequisite step). This ensures the simplest packaging process you've ever seen. GitHub Repository. If you can, please give it a star. Thank you!

This automated packaging tool is based on the pyinstaller Python library.

The packaging tool relies on conda for creating and managing virtual environments, so the prerequisite step is installing conda (skip this step if you already have it installed).

This program only supports packaging in Windows environments, specifically for creating .exe files.

2. Prerequisite Steps

Step 1: Download from the Official Website

Go to the Anaconda official website and download anaconda. Enter your email and click Submit.

Image description

Step 2: Choose Anaconda or Miniconda

Click Download, and Anaconda will select the appropriate platform version for you.

Image description

If you want to use conda to manage dependencies, downloading Anaconda is a great choice (it even includes a graphical interface for dependency management). If you're just downloading conda to package Python files, scroll down on the page to find the "Miniconda installer" – this is a better lightweight choice (but lacks a graphical interface).

Step 3: Confirm Installation

Open the downloaded installer and use the default settings. You can change the installation path if you want. After installation, check the Windows Start Menu for Anaconda Prompt or Anaconda Powershell Prompt. If they're present, the installation is successful.

Image description

3. Using the Automated Packaging Tool

Step 1: Choose a Tool

The GitHub Repository provides two tools: EasyPackager and PeasyPackager. If you don’t need to add icons (.ico) or package multiple Python files into a single executable, use EasyPackager. Here, we explain how to use EasyPackager.

Note: If conda is installed on the C drive (default installation), remember to run the program as an administrator to avoid permission issues.

Step 2: Use the Base Environment or Create a New Environment

Open EasyPackager_GUI.exe (GUI version recommended). The first option lets you choose between the base environment or creating a new environment.

When using the base environment, the generated executable file tends to be large because Pyinstaller packages all libraries and modules in the environment, even unused ones. This not only makes the executable bulky but also slows its execution. Therefore, it’s highly recommended to use the second method – creating a new environment. Choose new.

Image description

Step 3: Select the File to Package

Click "Select file" and choose the Python file to package.

Image description

Step 4: Enter Python Version and Dependencies

If using the base environment, skip this step. If you selected new, fill in the following fields:

Image description

  1. Enter the Python version: Specify the Python version required for your program (e.g., 3.10).
  2. Enter the dependencies' package names: List the required dependency packages (not module names), separated by spaces. For example, if your program uses cv2 and numpy, enter:
opencv-python numpy
Enter fullscreen mode Exit fullscreen mode

Leave this blank if no additional dependencies are needed.

Step 5: Choose Radio Button Options

  1. Generates a single executable file: Select this to generate a standalone .exe file. If unchecked, additional .dll files will be created alongside the .exe. Strongly recommended to select this.
  2. Runs the program while opening a command line window: Select this if your program lacks a GUI but includes interactive parts (e.g., input()).

Image description
This is an example. The black window is command line, the white one is GUI.

If unsure, select both options.

Step 6: Execute

Click the "Execute" button. A command line window will open. Ensure the window stays on top (do not use your computer for other tasks during execution). When the command line shows “Finish packaging, you can exit right now!”, you can close it.

Image description

Step 7: Locate the .exe File

After execution, navigate to the Python file's directory. A dist folder will contain the executable file. Other generated files can be deleted.

Image description

If you didn’t select "Generates a single executable file," the folder will include the .exe file and a folder with dynamic link files.

4. About PeasyPackager

PeasyPackager is an advanced version of EasyPackager. It supports adding icons (.ico) to the program and packaging multiple Python files into a single executable.

PeasyPackager is similar to EasyPackager, with a few additional features:

1. Add Program Icon

The “Add icon for the program” option allows users to add an icon to the packaged program. It only supports .ico files. Select this option and click "Select file" to choose the icon file.

Image description

2. Package Multiple Python Files

The “Package multiple Python files” option allows users to package multiple .py files. Simply select their paths.
Image description

Top comments (0)