Use pyinsatller to publish a python program project as an executable file

Use pyinsatller to publish a python program project as an executable file

 

Python is a scripting language, that is, an interpreted language. It needs an interpreter to interpret it before it can be executed. The Python source code needs to run on the Python virtual machine, but the programs we make cannot be used by users. At that time, he was asked to install a Python environment, so today we explain how to package a Python program into an exe executable file, you can use the pyinsatller module. Official website http://www.pyinstaller.org/

First introduce the installation and use of the pyinsatller module, and then give a release example.

Install the pyinsatller module

Open a command line window and enter the following command

pip install pyinstaller

Wait for pyinstaller to be installed, see the figure below:

 

The pyinstaller command briefly uses the format:

pyinstaller -p 路径 -F -w [-i icofile] filename

among them:

-p path, indicating the location of the dependent module (or library), its location is in the site-packages folder of the Python installation path,

[View the Python installation path, see https://blog.csdn.net/cnds123/article/details/101546889

To solve the "ModuleNotFindError" that appears when running the exe program after pyinstaller is packaged, refer to the exe executable file packaged by pyinstaller, the module cannot be found. Solution https://blog.csdn.net/weixin_44297303/article/details/99059299  ]

-F: Only a file is generated, other information is not exposed, and the startup is slower. -D: Generate a folder with multi-file mode and quick start.

-w: Package in window mode without displaying the console.

filename indicates that your is the name of the main py program, which can include a path.

-w means to hide the command line window when the program is running (there will be a black window without -w)

The parameters in brackets are optional parameters. -i icofile means to add an icon to the program. The icon must be in .ico format. icofile means the location of the icon. It is recommended to put it directly in the program folder, so that the file name is written directly when packaging. Great.

 

An example of publishing is given below

The structure of the project I want to publish is as follows:

The bird.py source code file is as follows:

import pygame
pygame.init()#初始化操作
#保存窗口大小
width,height=600,400
screen=pygame.display.set_mode([width,height])#创建游戏窗口

#设置窗口标题
pygame.display.set_caption("愤怒的小鸟")

#加载小鸟素材
player=pygame.image.load("xiaoniao.png")

#获取图像矩形位置
rect=player.get_rect()

#声明XY运动速度的列表
speed = [3,2]

left_head = pygame.transform.flip(player,True,False)
right_head = player
#无限循环
while True:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            exit()
        if event.type ==pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player = left_head #小鸟的头向左
                speed=[-2,1]
            if event.key == pygame.K_RIGHT:
                player = right_head #小鸟的头向左
                speed=[2,1]
            if event.key == pygame.K_UP:
                player = left_head #小鸟的头向左
                speed=[2,-1]
            if event.key == pygame.K_DOWN:
                player = right_head #小鸟的头向左
                speed=[2,1]

    rect =rect.move(speed)
    if rect.right>width or rect.left<0:
        #将图片水平翻转             反转对象 是否水平反转 是否垂直翻转
        player = pygame.transform.flip(player,True,False)
        speed[0]=-speed[0]

    if rect.bottom>height or rect.top<0:
        speed[1]=-speed[1]



    screen.fill((255,255,255))
    screen.blit(player,rect)
    pygame.display.update()
    pygame.time.delay(10)

xiaoniao.png picture:

It is recommended (this is not necessary) to first run the test project with IDEL to see if it is normal

If there is an error prompting
ModuleNotFoundError: No module named'pygame'
, please install pygame
pip install pygame

If it runs normally, see the figure below:

 

Verify that the program is correct, you can use pyinsatller to publish it as an executable file:

In the cmd (command line) window, use the cd command to enter the program folder

CD /DD:\Python Angry Birds touches the edge and rebounds

 

Enter the following command to start conversion

pyinstaller -p C:\Users\Wang\AppData\Local\Programs\Python\Python38\Lib\site-packages -F -w bird.py

Because the pygame module (package) is used in the source code, use -p C:\Users\Wang\AppData\Local\Programs\Python\Python38\Lib\site-packages to indicate the location of the pygame module (package).

After the input is completed, press Enter, and the automatic packaging will start. The first packaging process may be slow

Please note that the appending archive indicates the location of the generated exe file.

After successful packaging, three folders are generated in the current directory, and the generated EXE file is in the dist folder. See the picture below:

 

[Prompt, the converted exe file is very large

Reduction method: use which function to import which function, do not import the entire library, because this will pack the entire library in, and unused library functions will take up a lot of space.

 

How to run

Place the generated executable file , here is bird.exe and the resource file it uses-here is the image file xiaoniao.png , in the same directory, and then double-click bird.exe to see the effect.

 

Further study materials:

Pyinstaller official help document https://pyinstaller.readthedocs.io/en/stable/usage.html

Summary of Python packaging exe file methods [4 types] https://blog.csdn.net/lzy98/article/details/83246281

 

Guess you like

Origin blog.csdn.net/cnds123/article/details/115254418