使用pyinsatller将python程序项目发布为可执行文件

使用pyinsatller将python程序项目发布为可执行文件

 

Python是一种脚本语言,也就是解释型的语言,需要解释器来进行解释以后才可以执行,而Python源代码需要在Python虚拟机上面运行,但是我们做好的程序,不可能给用户使用的时候还让他安装一个Python环境,所以今天我们讲解如何将Python程序打包为exe可执行文件,可以用pyinsatller模块。官网 http://www.pyinstaller.org/

先介绍pyinsatller模块的安装与使用,然后给出一个发布示例。

安装pyinsatller模块

打开命令行窗口,输入如下指令

pip install pyinstaller

等待pyinstaller安装好,参见下图:

 

pyinstaller指令简要使用格式:

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

其中:

-p 路径,指明依赖的模块(或库)位置,其位置在Python安装路径的site-packages文件夹中,

【查看Python安装路径,参见 https://blog.csdn.net/cnds123/article/details/101546889

解决pyinstaller 打包后运行exe程序出现的"ModuleNotFindError" ,参见pyinstaller打包的exe执行文件,模块找不到问题解决方案https://blog.csdn.net/weixin_44297303/article/details/99059299 】

-F:仅仅生成一个文件,不暴露其他信息,启动较慢。-D:生成一个文件夹,里面是多文件模式,启动快。

-w:窗口模式打包,不显示控制台。

filename表示你的是主py程序名,可以带路径。

-w 表示隐藏程序运行时的命令行窗口(不加-w会有黑色窗口)

括号内的为可选参数,-i icofile表示给程序加上图标,图标必须为.ico格式,icofile表示图标的位置,建议直接放在程序文件夹里面,这样子打包的时候直接写文件名就好。

 

下面给出一个发布示例

我要发布的项目,结构如下:

bird.py源码文件如下:

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图片:

建议(这不是必须的)先用IDEL运行测试项目是否正常

若出现错误提示
ModuleNotFoundError: No module named 'pygame'
请安装pygame
pip install pygame

若正常运行,参见下图:

 

验证程序无误,就可以用pyinsatller发布为可执行文件了:

在cmd(命令行)窗口,使用cd指令进入程序文件夹

CD /D D:\Python愤怒小鸟触边反弹

 

输入以下指令,开始转换

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

因为源码中用到pygame 模块(包),所以使用-p C:\Users\Wang\AppData\Local\Programs\Python\Python38\Lib\site-packages 指出pygame 模块(包)位置。

输入完成,按回车,就会开始自动打包了,第一次打包过程可能比较缓慢

请留意appending archive 处指明了生成的exe文件的位置。

打包成功之后在当前目录下生成三个文件夹,生成的EXE文件就在dist文件夹中。参见下图:

 

【提示,转换的exe文件很大

减小的方法:用什么函数导什么函数,不要import整个库,因为这样会把整个库打包进去,没用到的库函数会占用了很大的空间。】

 

如何运行

将生成的可执行文件 ,这儿是bird.exe和它所用的资源文件——这里是图片文件xiaoniao.png,放在同一目录中,然后,双击bird.exe,就能看到效果了。

进一步学习资料:

Pyinstaller官方帮助文档https://pyinstaller.readthedocs.io/en/stable/usage.html

Python打包exe文件方法汇总【4种】https://blog.csdn.net/lzy98/article/details/83246281

 

猜你喜欢

转载自blog.csdn.net/cnds123/article/details/115254418