preamble
We sometimes write Python scripts to assist us in performing some repetitive operations. But these scripts can be a bit inconvenient to use in practice:
We usually need to go into a terminal or IDE to run a script (of course, there are ways to double-click on a script file to run it directly, but that's beyond the scope of today's discussion).
If the script is migrated to another computer, there is a high probability that the script will not run properly if the Python environment changes, for example, if Python is not installed on the new computer, or if there is a lack of packages called by the script.
At this time, we can package the script into an application that can run independently, point-and-click to solve the above problems. The third-party package Pyinstaller can be realized to package Python scripts into .exe format applications, now with an example to introduce the use of methods.
Let's start by describing the environment in which the development will take place.
As is customary for development, our Python scripts are developed in a virtual environment.
Path to the project: D:\myProject\
Virtual Environment Path: D:\myProject\virtualEnvironment\
Script storage path: D:\myProject\code\
First run the script in VS code to see the effect. This script can generate a scientific research software needs csv format data, with a simple GUI interface, easy to set the user to generate content.
All we have to do is package script files in .py format, which depend on the development environment, into a standalone application in .exe format that can run on its own.
Installing Pyinstaller
The scripts are developed in a virtual environment, so we install Pyinstaller in this virtual environment as well. to be on the safe side, we force the pip from the virtual environment to be used for installation in the terminal:
D:\myProject\virtualEnvironment\Scripts\python -m pip install pyinstaller
After the installation is complete, you can see some new applications in the "Scripts" directory of the virtual environment, which are used for packaging scripts.
Packaging scripts into exe applications
The most basic command to package a Python script into an exe application using Pyinstaller is:
pyinstaller
In order to increase the success rate of packing and to make the packing process clearer and more manageable, I recommend the following steps.
First open a terminal and cd to the directory where the exe application is stored. For example, let's create a new "exe" folder in the project directory and cd to it:
cd D:\myProject\exe\
Then the command pyinstaller packages the script:
D:\myProject\virtualEnvironment\Scripts\pyinstaller -F D:\myProject\code\
In the above instruction, the
D:\myProject\virtualEnvironment\Scripts\pyinstallerbepyinstallerThe full path of the。
D:\myProject\code\ is the full path to the Python script being packaged.
D:\myProject\exe\ is the directory where the generated exe application is stored.
The packing process is clear and unambiguous.
After the packaging is complete, we check the D:\myProject\exe\ directory to see some newly created folders, and enter the D:\myProject\exe\dist directory to see the generated applications.
Double-click on the application and the script starts successfully.
In this way, the required environment for the script has been packaged into the .exe program, and the script will no longer need to rely on an external Python development environment to run. Copy it to another computer, and the script will start without any problems, even if the computer does not have Python installed.
Sometimes Python scripts need to call external files, which involves the issue of "root directory" and "relative path". After testing, when we package the script into an exe application, the directory where the exe application is located is the "root directory".
Advanced: eliminating command windows, customizing icons
The exe application generated by the above operation will open a black command window at startup, which is unattractive, we can add -w when executing the packing command to eliminate this command window:
D:\myProject\virtualEnvironment\Scripts\pyinstaller -F -w D:\myProject\code\
This generates an exe application that doesn't have this black command window at startup.
In addition, the icon of the generated exe application can be customized to support .ico format icon files. Just add -i to specify the icon file when executing the package command:
D:\myProject\virtualEnvironment\Scripts\pyinstaller -F -w -i D:\myProject\ D:\myProject\code\
The icon of the exe application generated in this way is our customized icon.
summarize
to this article on how to package Python scripts into exe application is introduced to this article, more related Python packaged into exe content, please search my previous posts or continue to browse the following related articles I hope you will support me in the future!