In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to package Python scripts into exe applications". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to package Python scripts into exe applications" can help you solve the problem.
Preface
We sometimes write Python scripts to assist us in performing repetitive operations. But there are some inconveniences in using these scripts in practice:
We usually need to go to the terminal or IDE to run the script (of course, there are ways to double-click the script file to run it directly, but this is beyond the scope of today's discussion).
If you migrate the script to another computer, there is a good chance that the script will not work properly due to changes in the Python environment, such as no Python installed on the new computer, or lack of packages called by the script.
At this point, we can package the script into an application that can run independently and click on it to solve the above problem. The third-party package Pyinstaller can implement applications that package Python scripts into .exe format, and now use an example to show how to use it.
First of all, introduce the development environment.
According to the development habit, our Python script is 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 how it works. This script can generate csv format data for scientific research software, and has a simple GUI interface to facilitate users to set up the generated content.
All we have to do is package the script files in .py format that depend on the development environment into a .exe format application that can run independently.
Install Pyinstaller
The script is developed in a virtual environment, so we also install Pyinstaller in this virtual environment. To be on the safe side, force the installation of pip in the virtual environment 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 in the virtual environment, where pyinstaller.exe is used to package scripts.
Package the script into an exe application
To package Python scripts into exe applications using Pyinstaller, the most basic commands are:
Pyinstaller codeName.py
In order to improve the success rate of packaging and make the packaging process more clear and controllable, I recommend the following steps.
First open a terminal and cd to the directory where the exe application is stored. For example, we create a new "exe" folder in the project directory and cd to this point:
Cd D:\ myProject\ exe\
Then command pyinstaller to package the script:
D:\ myProject\ virtualEnvironment\ Scripts\ pyinstaller-F D:\ myProject\ code\ myScript.py
In the above instructions
D:\ myProject\ virtualEnvironment\ Scripts\ pyinstaller is the full path to pyinstaller.
D:\ myProject\ code\ myScript.py is the full path to the packaged Python script.
D:\ myProject\ exe\ is the directory where the generated exe application is stored.
The packing process is clear and clear.
After the packaging is complete, we look at the D:\ myProject\ exe\ directory, we can see some new folders, go to the D:\ myProject\ exe\ dist directory, you can see the generated myScript.exe application.
Double-click the application and the script starts successfully.
In this way, the environment required for the script has been packaged into a .exe program, and the script will no longer need to rely on the external Python development environment. Copy myScript.exe to another computer, and the script starts smoothly even if the computer does not have Python installed.
Python scripts sometimes need to call external files, which involves "root" and "relative path" issues. After testing, when we package the script into an exe application, the directory where the exe application is located is the "root directory".
Advanced: eliminate command windows, custom icons
The exe application generated by the above operations will open a black command window when it starts. You can eliminate this command window by adding-w when executing the packaging command:
D:\ myProject\ virtualEnvironment\ Scripts\ pyinstaller-F-w D:\ myProject\ code\ myScript.py
The generated exe application will not have this black command window when it starts.
In addition, the icons of the generated exe applications can also be customized to support icon files in .ico format. Add-I to specify the icon file when you execute the packaging command:
D:\ myProject\ virtualEnvironment\ Scripts\ pyinstaller-F-w-I D:\ myProject\ myIcon.ico D:\ myProject\ code\ myScript.py
The icon of the exe application generated in this way is our custom icon.
That's all for "how to package Python scripts into exe applications". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 271
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.