文章目录: 一. PyInstaller简介 二. PyInstaller在Windows下的安装 三. 打包 四. 小实例(Windows下)
附加:pyinstaller简介
PyInstaller能够将Python脚本打包成可执行程序,使得在没有Python环境的机器上也可以运行这些程序。
PyInstaller的最新版本是3.1.1,支持Python 2.7和Python 3.3及以上版本。它可以在Windows、Mac和Linux操作系统上运行。但需要注意的是,PyInstaller不支持跨平台编译,这意味着在Windows下生成的exe文件只能在Windows下运行,Linux下生成的只能在Linux下运行。
二. PyInstaller在Windows下的安装
在Windows下安装PyInstaller可以通过命令pip install pyinstaller来完成。PyInstaller需要PyWin32的支持,在通过pip安装PyInstaller时,如果未找到PyWin32,会自动安装pypiwin32。
立即学习“Python免费学习笔记(深入)”;
当看到提示“Successfully installed pyinstaller-3.1.1 pypiwin32-219”时,表示安装成功。
三. 打包
打包后的应用程序不包含任何源代码,但会包含脚本的.pyc文件。
基本语法为:pyinstaller options myscript.py
例如:pyinstaller --paths="D:\Queena" guess_exe.py
四. 小实例(Windows下)
编写一个名为guess_exe.py的游戏文件,代码如下:
# -*- coding:utf-8 -*- # 摇3次骰子,当总数total,3 def roll_dice(): '''摇骰子''' points_list = [] while times > 0: number = random.randrange(1,7) points_list.append(number) times -= 1 return points_list <p>def roll_result(total): '''判断是大是小''' is_big = 11 <= total <= 18 is_small = 3 <= total <= 10 if is_big: return 'Big' elif is_small: return 'Small'</p><p>def enter_stake(current_money): '''下注''' stake = int(input('请下注:')) if stake > current_money: print('余额不足') return enter_stake(current_money) rate = 1.5 if stake > current_money/2 else 2 return stake, rate</p><p>def settlement(boo, points_list, current_money, stake, rate): '''结算''' if boo: current_money += stake * rate print('你赢了,恭喜!你的余额是:', current_money) else: current_money -= stake print('你输了,很遗憾!你的余额是:', current_money) return current_money</p><p>def sleep_second(second=3): '''暂停''' time.sleep(second)</p><p>def start_game(): '''开始游戏''' current_money = 1000 while current_money > 0: print('>>>>>>>>>>>>>>>>>>>') 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)</p><p>if <strong>name</strong> == '<strong>main</strong>': start_game()
之后在命令行中,切换到guess_exe.py所在的目录,输入以下命令:
pyinstaller --onefile --nowindowed --icon="D:QueenaPyCharmProjectsdist1computer_three.ico" guess_exe.py
执行后,当前目录下会生成build文件夹、dist文件夹和.spec文件。dist文件夹中包含了guess_exe.exe可执行文件。
附加:
定位到pyinstaller.exe所在的文件夹(通常在Python的“scripts”文件夹下)。(温馨提示:在cmd中使用Tab键可以进行路径补全。)
在命令中添加你要转换的文件路径(路径与pyinstaller.exe之间用空格隔开)。
pyinstaller.exe后面如果加上-F参数,会打包为一个exe文件(文件会较大);如果不加-F参数,会生成多个库文件。加上-w参数会打包为没有cmd窗口的exe文件,不加-w参数运行时会出现cmd窗口。(加不加参数根据个人喜好而定。)
加-F参数的效果
注意指令区分大小写。这里是大写。使用-F指令可以将应用打包成一个独立的exe文件,否则会生成一个包含各种dll和依赖文件的文件夹。
这个指令后面可以增加PyInstaller搜索模块的路径。因为应用打包涉及的模块很多。这里可以自己添加路径。不过经过测试,site-packages目录下的模块都可以被识别,不需要再手动添加。
以上就是[272]如何把Python脚本导出为exe程序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号