文章目录:一. 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。
立即进入“豆包AI人工智官网入口”;
立即学习“豆包AI人工智能在线问答入口”;
![[272]如何把Python脚本导出为exe程序](https://www.chuangxiangniao.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
![[272]如何把Python脚本导出为exe程序](https://www.chuangxiangniao.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
当看到提示“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,3def roll_dice(): '''摇骰子''' points_list = [] while times > 0: number = random.randrange(1,7) points_list.append(number) times -= 1 return points_listdef roll_result(total):'''判断是大是小'''is_big = 11 <= total <= 18is_small = 3 <= total <= 10if is_big:return 'Big'elif is_small:return 'Small'
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 2return stake, rate
def settlement(boo, points_list, current_money, stake, rate):'''结算'''if boo:current_money += stake * rateprint('你赢了,恭喜!你的余额是:', current_money)else:current_money -= stakeprint('你输了,很遗憾!你的余额是:', current_money)return current_money
def sleep_second(second=3):'''暂停'''time.sleep(second)
def start_game():'''开始游戏'''current_money = 1000while 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_resultcurrent_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:QueenaPyCharmProjectsdist1computer_three.ico" guess_exe.py
执行后,当前目录下会生成build文件夹、dist文件夹和.spec文件。dist文件夹中包含了guess_exe.exe可执行文件。
![[272]如何把Python脚本导出为exe程序](https://www.chuangxiangniao.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
附加:
定位到pyinstaller.exe所在的文件夹(通常在Python的“scripts”文件夹下)。(温馨提示:在cmd中使用Tab键可以进行路径补全。)
在命令中添加你要转换的文件路径(路径与pyinstaller.exe之间用空格隔开)。
pyinstaller.exe后面如果加上-F参数,会打包为一个exe文件(文件会较大);如果不加-F参数,会生成多个库文件。加上-w参数会打包为没有cmd窗口的exe文件,不加-w参数运行时会出现cmd窗口。(加不加参数根据个人喜好而定。)
加-F参数的效果
不加-F参数的效果
不加-w参数的效果(加-w参数后,就不会有后面的黑框了)
-F指令
注意指令区分大小写。这里是大写。使用-F指令可以将应用打包成一个独立的exe文件,否则会生成一个包含各种dll和依赖文件的文件夹。
![[272]如何把Python脚本导出为exe程序](https://www.chuangxiangniao.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
-p指令
这个指令后面可以增加PyInstaller搜索模块的路径。因为应用打包涉及的模块很多。这里可以自己添加路径。不过经过测试,site-packages目录下的模块都可以被识别,不需要再手动添加。
![[272]如何把Python脚本导出为exe程序](https://www.chuangxiangniao.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
以上就是[272]如何把Python脚本导出为exe程序的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/26631.html
微信扫一扫
支付宝扫一扫