如何把Python腳本導出爲exe程序

 

一.pyinstaller簡介

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

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

二.pyinstaller在windows下的安裝

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


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

三.打包

打包的app裏並不包含任何源碼,但將腳本的.pyc文件打包了。python2.7

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

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

四.小實例(windows下)

寫好遊戲文件guess_exe.py,代碼以下:spa

__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。

相關文章
相關標籤/搜索