Python—腳本程序生成exe可執行程序(pyinstaller)


一、pyinstaller的簡介

Python是一個腳本語言,被解釋器解釋執行。它的發布方式:

  • .py文件:對於開源項目或者源碼沒那么重要的,直接提供源碼,需要使用者自行安裝Python並且安裝依賴的各種庫。(Python官方的各種安裝包就是這樣做的)。
  • .pyc文件:有些公司或個人因為機密或者各種原因,不願意源碼被運行者看到,可以使用pyc文件發布,pyc文件是Python解釋器可以識別的二進制碼,故發布后也是跨平台的,需要使用者安裝相應版本的Python和依賴庫。
  • 可執行文件:對於一些小白用戶,最簡單的方式就是提供一個可執行文件,只需要把用法告訴Ta即可。比較麻煩的是需要針對不同平台需要打包不同的可執行文件(Windows,Linux,Mac,...)。

二、pyInstaller的原理簡介

三、pyinstaller的安裝

[root@localhost ~]# pip install pyinstaller

四、小實例(windows下)

# -*- coding:utf-8 -*-
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))
    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函數的時候,就不帶-w,不然會報錯。
E:\>pyinstaller -F -w test.py   

https://www.wukong.com/answer/6732408567304814861/

https://www.cnblogs.com/robinunix/p/8426832.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM