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__ = 'zhou' # -*- 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:\Queena\PyCharmProjects\dist1\computer_three.ico" guess_exe.py
就會在當前檔案下形成build資料夾、dist資料夾和.spec檔案。 dist裡就是guess_exe.exe可執行檔案。
[外鏈圖片轉存失敗(img-NSV511rc-1562767762570)(https://upload-images.jianshu.io/upload_images/6152595-56dc5aad9152513e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]
1、安裝pyinstaller(需要先安裝pip)、再:pip install pyinstaller
(由於我事先安裝了pyinstaller
,為了方便就解除安裝了,不知道影不影響顯示。但安裝成功後會有“Successfully installed pyinstaller”的提示)
2、定位到pyinstaller.exe所在資料夾(一般再python下的“scripts”資料夾下)
(溫馨提示:再cmd下tab鍵又補全功能哦)
3、再新增上你要轉換的檔案地址(兩者之間有空格)
pyinstaller.exe後面如果加上-F就是打包為一個exe檔案(檔案會比較大),如果不加就會有很多庫檔案;加上-w就是打包為沒有cmd視窗的exe,不加執行時就會出現cmd視窗。(加不加憑個人喜好)
4. 加-F的效果
不加-F
不加-w的效果
(加-w的話,就沒有後面的那個黑框了
1、-F指令
注意指令區分大小寫。這裡是大寫。使用-F指令可以把應用打包成一個獨立的exe檔案,否則是一個帶各種dll和依賴檔案的資料夾
2、-p指令
這個指令後面可以增加pyinstaller
搜尋模組的路徑。因為應用打包涉及的模組很多。這裡可以自己新增路徑。不過經過筆者測試,site-packages
目錄下都是可以被識別的,不需要再手動新增
補充:如何將python的.py檔案轉換為可執行的.exe檔案。
首先,我寫了一個print(“hello,world”).py檔案。命名為hello.py儲存在我的電腦C盤的C:\Users\ly目錄下如圖所示。
ps:儘量選擇在這個資料夾下,如果選擇其他盤的資料夾下,生成的.exe的dist資料夾也會出現在這個c盤的路徑下,而且如果儲存在其他盤下有時候還會出錯,不好用。
利用pip安裝python的工具庫pyinstaller。
pip install pyinstaller
安裝成功後
在命令視窗輸入:pyinstaller -F C:\Users\ly\hello.py
注意 F 一定要大寫
然後就會在這個路徑下的dist資料夾下找到這和同名的hello.exe檔案。
總結
到此這篇關於Python指令碼匯出為exe程式的方法的文章就介紹到這了,更多相關Python匯出exe程式內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!