學員肖杰:如何把Python腳本導出為exe程序

一.pyinstaller簡介

pyinstaller將Python腳本打包成可執行程序,使在沒有Python環境的機器上運行

最新版是pyinstaller 3.1.1。支持python2.7和python3.3+。
可運行在Windows,Mac和Linux操作系統下。
但它不是跨編譯的,也就是說在Windows下用PyInstaller生成的exe只能運行在Windows下,在Linux下生成的只能運行在Linux下。

二.pyinstaller在windows下的安裝

使用命令pip install pyinstaller即可
在windows下,pyinstaller需要PyWin32的支持。當用pip安裝pyinstaller時未找到PyWin32,會自動安裝pypiwin32


出現Successfully installed pyinstaller-3.1.1 pypiwin32-219即表示安裝成功

三.打包

打包的app里并不包含任何源碼,但將腳本的.pyc文件打包了。

基本語法:
pyinstaller options myscript.py
常用的可選參數如下:
--onefile 將結果打包成一個可執行文件
--onedir 將所有結果打包到一個文件夾中,該文件夾包括一個可執行文件和可執行文件執行時需要的依賴文件(默認)
--paths=DIR 設置導入路徑
--distpath=DIR 設置將打包的結果文件放置的路徑
--specpath=DIR 設置將spec文件放置的路徑
--windowed 使用windows子系統執行,不會打開命令行(只對windows有效)
--nowindowed 使用控制臺子系統執行(默認)(只對windows有效)
--icon=<FILE.ICO> 將file.ico添加為可執行文件的資源(只對windows有效)

如pyinstaller --paths="D:\Queena" guess_exe.py

四.小實例(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,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:\Queena\PyCharmProjects\dist1\computer_three.ico" guess_exe.py

就會在當前文件下形成build文件夾、dist文件夾和.spec文件。
dist里就是guess_exe.exe可執行文件。


如果有打包錯誤,具體看build里的warn***.txt文檔,里面詳細記載了錯誤的原因。一般都是庫丟失。
spec文件告訴PyInstaller如何去處理腳本。它對腳本名以及大多數pyinstaller的可選參數進行加密。PyInstaller就是通過執行spec文件的內容來build the app。

五. 參加麻瓜編程心得:

我最大的感想是魔力教程的優美,它清晰、簡潔、易懂。
視頻學習過程中深刻體會到了編制者的用心,精美的配圖加上適宜的背景音樂,基礎知識和貼切的小項目,這一系列的配套成就了麻瓜的不凡。
成功的學會十萬數據的爬取之后,那種成就感簡直了哎呀,無法言喻。之后是數據的可視化還有各種圖形以及顯示在網頁上,這一連串的成就都讓我很開心,而且這個技能讓我在職業技術上有了很大的提升,以后跳槽我又有了資本,十分感謝麻瓜!

六. 參考網址:
http://pythonhosted.org/PyInstaller/
http://blog.csdn.net/zc02051126/article/details/8827868


想上手實戰的小白看過來,這里有網易云課堂上最暢銷的 Python 課程:Python 實戰:四周學會爬蟲系統

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 原文鏈接 我經常用python寫些腳本什么的,有時候腳本寫完以后,每次運行都得在IDE打開在運行,很麻煩,所以經常...
    g0閱讀 6,157評論 0 2
  • # Python 資源大全中文版 我想很多程序員應該記得 GitHub 上有一個 Awesome - XXX 系列...
    aimaile閱讀 26,537評論 6 427
  • GitHub 上有一個 Awesome - XXX 系列的資源整理,資源非常豐富,涉及面非常廣。awesome-p...
    若與閱讀 18,696評論 4 418
  • 環境管理管理Python版本和環境的工具。p–非常簡單的交互式python版本管理工具。pyenv–簡單的Pyth...
    MrHamster閱讀 3,823評論 1 61
  • 取自《web界面設計》
    蘿卜家長閱讀 501評論 0 3