Want to package python code as exe program? It only takes three steps

Recently, some friends asked me, can a project written in python be packaged into an exe program and executed on a computer without a python environment?

The answer is of course yes. Python has a third-party library called pyinstaller, which is specifically used to package python programs into exe programs.

Today, I will share with you how to package, you need to use pip to install before use.

Environment installation installation command: pip install pyinstaller

If the installation fails, remember to change the domestic source if the network timed out

Case presentation

1. Project source code

The project demonstrated here is a tank battle game written with python object-oriented knowledge and pygame when I was doing teaching and research. The code structure and running effect of the entire project are as follows:

2. Packaging commands

To package the above python project as an exe program, find the startup file of the project (the entry file of the project) at this time, and add the name of the startup file through pyinstaller to package the project, as follows:

In cmd, first switch to tank.py, the project startup file above in the project directory, and enter the packaging command:

After execution, the following files will be generated in the project directory:

· Build: Store temporary files produced during the packaging process

· Dist: The packaged project files including exe programs are here

· Tank.spec: packaged configuration file (you can configure the packaged program in this file)

Recommendation: 020 is continuously updated, the small circle of boutiques has new content every day, and the concentration of dry goods is extremely high.
There are everything you want to make connections and discuss technology!
Be the first to join the group and outperform your peers! (There is no fee for joining the group)
Click here to communicate and learn with Python developers.
Group number: 745895701
application and delivery :
Python software installation package, Python actual combat tutorial,
free collection of materials, including Python basic learning, advanced learning, crawling, artificial intelligence, automated operation and maintenance, automated testing, etc.

3. Use of the program:

After packaging, the dist folder is generated, and there is a folder with the same name as the packaging file. In this folder, you can find the packaged exe program. Double-click to run this program.

Common parameters

In the above packaged case, we did not add any parameters. In fact, pyinstaller has many parameter options when packaging. Here are some commonly used parameters.

· -D: The generated result is a directory, and various third-party dependencies, resources and exe are stored in this directory at the same time (this parameter is the default) pyinstaller tank.py -D parameter is the default, we did not add it when we packaged For any parameters, a folder is produced by default after packaging, and various third-party dependent resources and exe are all in one directory

· -F: The generated result is an exe file, and all third-party dependencies, resources and codes are packaged into the exe. If we only want to package as an exe program, we can use the -F parameter, and the product will be produced after packaging. There is only one exe file, and the resources are all in this exe pyinstaller -f tank.py Executing the above command will produce a [tank.exe] program

· -N: The generated .exe file and. The file name of the spec. If we want to modify the name of the exe program, we can specify it through the -n parameter; pyinstaller tank.py -n 战战-f

· -I: Specify the program icon for the exe If you want to add an icon for the exe program, you can specify an icon file in the ico format as the icon through the -i parameter. pyinstaller tank.py -i icon file.ico Executing the above command will produce a [tank.exe] program, the icon of the program is the ico picture you specify

For more parameters, if you are interested, you can see the official documentation of pyinstaller.

Guess you like

Origin blog.csdn.net/Python_xiaobang/article/details/112478946