Home > Backend Development > Python Tutorial > The Easiest Way to Package Your Python Files(Turn to .exe Files)

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

DDD
Release: 2024-12-27 19:24:16
Original
876 people have browsed it

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.

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

Step 2: Choose Anaconda or Miniconda

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

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

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.

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

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.

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

Step 3: Select the File to Package

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

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

Step 4: Enter Python Version and Dependencies

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

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

  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
Copy after login

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()).

The Easiest Way to Package Your Python Files(Turn to .exe Files)
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.

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

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.

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

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.

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

2. Package Multiple Python Files

The “Package multiple Python files” option allows users to package multiple .py files. Simply select their paths.
The Easiest Way to Package Your Python Files(Turn to .exe Files)

The above is the detailed content of The Easiest Way to Package Your Python Files(Turn to .exe Files). For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template