(转)Python_如何把Python脚本导出为exe程序
?原文地址:https://www.cnblogs.com/robinunix/p/8426832.html 一.pyinstaller简介Python是一个脚本语言,被解释器解释执行。它的发布方式:
本文主要就是介绍最后一种方式,.py和.pyc都比较简单,Python本身就可以搞定。将Python脚本打包成可执行文件有多种方式,本文重点介绍PyInstaller, ? PyInstaller的原理简介PyInstaller其实就是把python解析器和你自己的脚本打包成一个可执行的文件,和编译成真正的机器码完全是两回事,所以千万不要指望成打包成一个可执行文件会提高运行效率,相反可能会降低运行效率,好处就是在运行者的机器上不用安装python和你的脚本依赖的库。在Linux操作系统下,它主要用的 PyInstaller输入你指定的的脚本,首先分析脚本所依赖的其他脚本,然后去查找,复制,把所有相关的脚本收集起来,包括Python解析器,然后把这些文件放在一个目录下,或者打包进一个可执行文件里面。 可以直接发布输出的整个文件夹里面的文件,或者生成的可执行文件。你只需要告诉用户,你的应用App是自我包含的,不需要安装其他包,或某个版本的Python,就可以直接运行了。 需要注意的是,PyInstaller打包的执行文件,只能在和打包机器系统同样的环境下。也就是说,不具备可移植性,若需要在不同系统上运行,就必须针对该平台进行打包。 ? pyinstaller将Python脚本打包成可执行程序,使在没有Python环境的机器上运行 最新版是pyinstaller 3.1.1。支持python2.7和python3.3+。
回到顶部(go to top)
二.pyinstaller在windows下的安装使用命令pip install pyinstaller即可
?
?
?
?
? 出现Successfully installed pyinstaller-3.1.1 pypiwin32-219即表示安装成功
回到顶部(go to top)
三.打包打包的app里并不包含任何源码,但将脚本的.pyc文件打包了。 基本语法: 如pyinstaller --paths="D:Queena" guess_exe.py 示例: pyinstaller -F wxy_tool.py --distpath=wxy-mtt 四.小实例(windows下)写好游戏文件guess_exe.py,代码如下: __author__ = ‘qa-2‘ # -*- coding:utf-8 -*- # 摇3次骰子,当总数total,3<=total<=10时为小,11<=total<=18为大 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,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文件。
?
?
? 如果有打包错误,具体看build里的warn***.txt文档,里面详细记载了错误的原因。一般都是库丢失。 ?Python学习资料 1)https://www.liaoxuefeng.com/wiki/897692888725344/945671347631136 2)https://www.runoob.com/python3/python3-basic-syntax.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |