[272]如何把Python脚本导出为exe程序

pyinstaller简介

pyinstaller将Python脚本打包成可执行程序,使在没有Python环境的机器上运行

最新版是pyinstaller 6.12.0,可运行在Windows,Mac和Linux操作系统下。 但它不是跨编译的,也就是说在Windows下用PyInstaller生成的exe只能运行在Windows下,在Linux下生成的只能运行在Linux下。

pyinstaller安装

安装 PyInstaller 模块与安装其他 Python 模块一样,使用 pip 命令安装即可。在命令行输入如下命令:

pip install pyinstaller

强烈建议使用 pip 在线安装的方式来安装 PyInstaller 模块,不要使用离线包的方式来安装,因为 PyInstaller 模块还依赖其他模块,pip 在安装 PyInstaller 模块时会先安装它的依赖模块。

运行上面命令,应该看到如下输出结果:

Successfully installed pyinstaller-x.x.x

其中的 x.x.x 代表 PyInstaller 的版本。

在 PyInstaller 模块安装成功之后,在 Python 的安装目录下的 Scripts(D:\Python\Python36\Scripts) 目录下会增加一个 pyinstaller.exe 程序,接下来就可以使用该工具将 Python 程序生成 EXE 程序了。

打包

打包的app里并不包含任何源码,但将脚本的.pyc文件打包了。

基本语法: pyinstaller options myscript.py

PyInstaller 不仅支持 -F、-D 选项,而且也支持如下表所示的常用选项。

参数描述
-h,–help查看该模块的帮助信息
-F,–onefile产生单个的可执行文件
-D,–onedir产生一个目录(包含多个文件)作为可执行程序
–distpath=DIR设置将打包的结果文件放置的路径
–specpath=DIR设置将spec文件放置的路径
–icon=<FILE.ICO>将file.ico添加为可执行文件的资源(只对windows有效)
-a,–ascii不包含 Unicode 字符集支持
-d,–debug产生 debug 版本的可执行文件
-w,–windowed,–noconsolc指定程序运行时不显示命令行窗口(仅对 Windows 有效)
-c,–nowindowed,–console指定使用命令行窗口运行程序(仅对 Windows 有效)
-o DIR,–out=DIR指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件
-p DIR,–paths=DIR设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径
-n NAME,–name=NAME指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字

在表中列出的只是 PyInstaller 模块所支持的常用选项,如果需要了解 PyInstaller 选项的详细信息,则可通过 pyinstaller -h 来查看。

picture.ico为图标:
PyInstaller -F -i picture.ico -n noPac.exe noPac.py
打包成独立exe:
PyInstaller -F --version-file ver.txt noPac.py
# 多文件
pyinstaller -D noPac.py
# 单个可执行文件
pyinstaller -F noPac.py
加密打包exe(加密只针对依赖库),但是要安装tinyaes:pip install tinyaes
pyinstaller -F --key 123456 xxx.py


pyinstaller -F -w --icon="D:\Queena\PyCharmProjects\dist1\computer_three.ico" --paths="D:\Queena" guess_exe.py

参数说明

pyinstaller.exe后面如果加上-F就是打包为一个exe文件(文件会比较大),如果不加就会有很多库文件;加上-w就是打包为没有cmd窗口的exe,不加运行时就会出现cmd窗口。

  • 加-F的效果

  • 不加-F

  • 不加-w的效果(加-w的话,就没有后面的那个黑框了)

  • -F指令
    注意指令区分大小写。这里是大写。使用-F指令可以把应用打包成一个独立的exe文件,否则是一个带各种dll和依赖文件的文件夹

  • -p指令
    这个指令后面可以增加pyinstaller搜索模块的路径。因为应用打包涉及的模块很多。这里可以自己添加路径。不过经过笔者测试,site-packages目录下都是可以被识别的,不需要再手动添加。

小实例(windows下)

写好游戏文件guess_exe.py,代码如下:

# -*- coding:utf-8 -*-
# 摇3次骰子,当总数total,3<=total<=10时为小,11<=total<=18为大
__author__ = 'zhou'

import random
import time


def enter_stake(current_money):
    '''输入小于结余的赌资及翻倍率,未考虑输入type错误的情况'''
    stake = int(input('How much you wanna bet?(such as 1000):'))
    rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))
    small_compare = current_money < stake * rate
    
    while small_compare == True:
        stake = int(input('You has not so much money ${}!How much you wanna bet?(such as 1000):'.format(stake * rate)))
        rate = int(input("What multiplier do you want?你想翻几倍?(such as 2):"))
        small_compare = current_money < stake * rate

    return stake,rate


def roll_dice(times = 3):
    '''摇骰子'''
    print('<<<<<<<<<< Roll The Dice! >>>>>>>>>>')
    points_list = []
    while times > 0:
        number = random.randrange(1,7)
        points_list.append(number)
        times -= 1
    
    return points_list


def roll_result(total):
    '''判断是大是小'''
    is_big = 11 <= total <= 18
    is_small = 3 <= total <= 10
    if is_small:
        return 'Small'
    elif is_big:
        return 'Big'


def settlement(boo,points_list,current_money,stake = 1000,rate = 1):
    '''结余'''
    increase = stake * rate
    if boo:
        current_money += increase
        print('The points are ' + str(points_list) + ' .You win!')
        print('You gained $' + str(increase) + '.You have $' + str(current_money) + ' now.' )
    else:
        current_money -= increase
        print('The points are ' + str(points_list) + ' .You lose!')
        print('You lost $' + str(increase) + '.You have $' + str(current_money) + ' now.' )
   
    return current_money


def sleep_second(seconds=1):
    '''休眠'''
    time.sleep(seconds)

# 开始游戏
def start_game():
    '''开始猜大小的游戏'''
    current_money = 1000
    print('You have ${} now.'.format(current_money))
    sleep_second()
    while current_money > 0:
        print('<<<<<<<<<<<<<<<<<<<< Game Starts! >>>>>>>>>>>>>>>>>>>>')
        your_choice = input('Big or Small: ')
        choices = ['Big','Small']
        if your_choice in choices:
            stake,rate = enter_stake(current_money)
            points_list = roll_dice()
            total = sum(points_list)
            actual_result = roll_result(total)
            boo = your_choice == actual_result
            current_money = settlement(boo,points_list,current_money,stake,rate)
        else:
            print('Invalid input!')
    else:
        sleep_second()
        print('Game Over!')
    sleep_second(2)


if __name__ == '__main__':
    start_game()

之后命令行,切换到guess_exe.py的目录下, 输入:

pyinstaller --onefile --nowindowed --icon="D:\Queena\PyCharmProjects\dist1\computer_three.ico" guess_exe.py

就会在当前文件下形成build文件夹、dist文件夹和.spec文件。 dist里就是guess_exe.exe可执行文件。

参考:https://blog.youkuaiyun.com/pythonhy/article/details/133824995
https://www.jb51.net/python/318674nyf.htm

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值