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.
Go to the Anaconda official website and download anaconda. Enter your email and click Submit.
Click Download, and Anaconda will select the appropriate platform version for you.
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).
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 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.
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.
Click "Select file" and choose the Python file to package.
If using the base environment, skip this step. If you selected new, fill in the following fields:
opencv-python numpy
Leave this blank if no additional dependencies are needed.
This is an example. The black window is command line, the white one is GUI.
If unsure, select both options.
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.
After execution, navigate to the Python file's directory. A dist folder will contain the executable file. Other generated files can be deleted.
If you didn’t select "Generates a single executable file," the folder will include the .exe file and a folder with dynamic link files.
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:
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 “Package multiple Python files” option allows users to package multiple .py files. Simply select their paths.
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!