python代码生成可执行程序_Python—脚本程序生成exe可执行程序(pyinstaller)
一、pyinstaller的簡(jiǎn)介
Python是一個(gè)腳本語(yǔ)言,被解釋器解釋執(zhí)行。它的發(fā)布方式:
.py文件:對(duì)于開(kāi)源項(xiàng)目或者源碼沒(méi)那么重要的,直接提供源碼,需要使用者自行安裝Python并且安裝依賴的各種庫(kù)。(Python官方的各種安裝包就是這樣做的)。
.pyc文件:有些公司或個(gè)人因?yàn)闄C(jī)密或者各種原因,不愿意源碼被運(yùn)行者看到,可以使用pyc文件發(fā)布,pyc文件是Python解釋器可以識(shí)別的二進(jìn)制碼,故發(fā)布后也是跨平臺(tái)的,需要使用者安裝相應(yīng)版本的Python和依賴庫(kù)。
可執(zhí)行文件:對(duì)于一些小白用戶,最簡(jiǎn)單的方式就是提供一個(gè)可執(zhí)行文件,只需要把用法告訴Ta即可。比較麻煩的是需要針對(duì)不同平臺(tái)需要打包不同的可執(zhí)行文件(Windows,Linux,Mac,...)。
二、pyInstaller的原理簡(jiǎn)介
三、pyinstaller的安裝
[root@localhost ~]# pip install pyinstaller
四、小實(shí)例(windows下)
# -*- coding:utf-8 -*-
import random
import time
def enter_stake(current_money):
'''輸入小于結(jié)余的賭資及翻倍率,未考慮輸入type錯(cuò)誤的情況'''
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):
'''結(jié)余'''
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():
'''開(kāi)始猜大小的游戲'''
current_money = 1000
print('You have ${} now.'.format(current_money))
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()
五、pyinstaller打包
E:\>pyinstaller -F test.py # 有input函數(shù)的時(shí)候,就不帶-w,不然會(huì)報(bào)錯(cuò)。
E:\>pyinstaller -F -w test.py
總結(jié)
以上是生活随笔為你收集整理的python代码生成可执行程序_Python—脚本程序生成exe可执行程序(pyinstaller)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: css修改select选择框option
- 下一篇: python时间序列进行线性插值_Pyt