Python腳本導(dǎo)出為exe程序的方法
一.pyinstaller簡介
pyinstaller將Python腳本打包成可執(zhí)行程序,使在沒有Python環(huán)境的機(jī)器上運(yùn)行
最新版是pyinstaller 3.1.1。支持python2.7和python3.3+。 可運(yùn)行在Windows,Mac和Linux操作系統(tǒng)下。 但它不是跨編譯的,也就是說在Windows下用PyInstaller生成的exe只能運(yùn)行在Windows下,在Linux下生成的只能運(yùn)行在Linux下。
二.pyinstaller在windows下的安裝
使用命令pip install pyinstaller
即可 在windows下,pyinstaller需要PyWin32的支持。當(dāng)用pip安裝pyinstaller時(shí)未找到PyWin32,會自動安裝pypiwin32
出現(xiàn)Successfully installed pyinstaller-3.1.1 pypiwin32-219即表示安裝成功
三.打包
打包的app里并不包含任何源碼,但將腳本的.pyc文件打包了。
基本語法: pyinstaller options myscript.py
常用的可選參數(shù)如下:
–onefile 將結(jié)果打包成一個(gè)可執(zhí)行文件
–onedir 將所有結(jié)果打包到一個(gè)文件夾中,該文件夾包括一個(gè)可執(zhí)行文件和可執(zhí)行文件執(zhí)行時(shí)需要的依賴文件(默認(rèn))
–paths=DIR 設(shè)置導(dǎo)入路徑
–distpath=DIR 設(shè)置將打包的結(jié)果文件放置的路徑
–specpath=DIR 設(shè)置將spec文件放置的路徑
–windowed 使用windows子系統(tǒng)執(zhí)行,不會打開命令行(只對windows有效)
–nowindowed 使用控制臺子系統(tǒng)執(zhí)行(默認(rèn))(只對windows有效)
–icon=<FILE.ICO> 將file.ico添加為可執(zhí)行文件的資源(只對windows有效)
如pyinstaller --paths=“D:\Queena” guess_exe.py
四.小實(shí)例(windows下)
寫好游戲文件guess_exe.py,代碼如下:
__author__ = 'zhou' # -*- coding:utf-8 -*- # 搖3次骰子,當(dāng)總數(shù)total,3<=total<=10時(shí)為小,11<=total<=18為大 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(): '''開始猜大小的游戲''' 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
就會在當(dāng)前文件下形成build文件夾、dist文件夾和.spec文件。 dist里就是guess_exe.exe可執(zhí)行文件。
[外鏈圖片轉(zhuǎn)存失敗(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鍵又補(bǔ)全功能哦)
3、再添加上你要轉(zhuǎn)換的文件地址(兩者之間有空格)
pyinstaller.exe后面如果加上-F就是打包為一個(gè)exe文件(文件會比較大),如果不加就會有很多庫文件;加上-w就是打包為沒有cmd窗口的exe,不加運(yùn)行時(shí)就會出現(xiàn)cmd窗口。(加不加憑個(gè)人喜好)
4. 加-F的效果
不加-F
不加-w的效果
(加-w的話,就沒有后面的那個(gè)黑框了
1、-F指令
注意指令區(qū)分大小寫。這里是大寫。使用-F指令可以把應(yīng)用打包成一個(gè)獨(dú)立的exe文件,否則是一個(gè)帶各種dll和依賴文件的文件夾
2、-p指令
這個(gè)指令后面可以增加pyinstaller
搜索模塊的路徑。因?yàn)閼?yīng)用打包涉及的模塊很多。這里可以自己添加路徑。不過經(jīng)過筆者測試,site-packages
目錄下都是可以被識別的,不需要再手動添加
補(bǔ)充:如何將python的.py文件轉(zhuǎn)換為可執(zhí)行的.exe文件。
首先,我寫了一個(gè)print(“hello,world”).py文件。命名為hello.py保存在我的電腦C盤的C:\Users\ly目錄下如圖所示。
ps:盡量選擇在這個(gè)文件夾下,如果選擇其他盤的文件夾下,生成的.exe的dist文件夾也會出現(xiàn)在這個(gè)c盤的路徑下,而且如果保存在其他盤下有時(shí)候還會出錯(cuò),不好用。
利用pip安裝python的工具庫pyinstaller。
pip install pyinstaller
安裝成功后
在命令窗口輸入:pyinstaller -F C:\Users\ly\hello.py
注意 F 一定要大寫
然后就會在這個(gè)路徑下的dist文件夾下找到這和同名的hello.exe文件。
總結(jié)
到此這篇關(guān)于Python腳本導(dǎo)出為exe程序的方法的文章就介紹到這了,更多相關(guān)Python導(dǎo)出exe程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中將腳本打包成獨(dú)立EXE的實(shí)現(xiàn)示例
- 詳解如何將Python可執(zhí)行文件(.exe)反編譯為Python腳本
- PyInstaller將Python腳本打包為.exe可執(zhí)行文件的步驟詳解
- 如何將python腳本打包為exe可執(zhí)行文件
- 將Python腳本打包成exe文件
- 如何將Python腳本打包成exe應(yīng)用程序介紹
- python腳本打包后無法運(yùn)行exe文件的解決方案
- 實(shí)例講解Python腳本成為Windows中運(yùn)行的exe文件
- 從原理到操作全解析Python腳本轉(zhuǎn)exe文件實(shí)戰(zhàn)指南
相關(guān)文章
python實(shí)現(xiàn)進(jìn)度條的多種實(shí)現(xiàn)
這篇文章主要介紹了python實(shí)現(xiàn)進(jìn)度條的多種實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04python防止隨意修改類屬性的實(shí)現(xiàn)方法
這篇文章主要介紹了python防止隨意修改類屬性的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python中使用Counter進(jìn)行字典創(chuàng)建以及key數(shù)量統(tǒng)計(jì)的方法
今天小編就為大家分享一篇Python中使用Counter進(jìn)行字典創(chuàng)建以及key數(shù)量統(tǒng)計(jì)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07python入門基礎(chǔ)之用戶輸入與模塊初認(rèn)識
Python的強(qiáng)大之處在于他有非常豐富和強(qiáng)大的標(biāo)準(zhǔn)庫和第三方庫,幾乎你想實(shí)現(xiàn)的任何功能都有相應(yīng)的Python庫支持。下面通過本文給大家介紹python入門基礎(chǔ)之用戶輸入與模塊初認(rèn)識,一起看看吧2016-11-11基于SQLAlchemy實(shí)現(xiàn)操作MySQL并執(zhí)行原生sql語句
這篇文章主要介紹了基于SQLAlchemy實(shí)現(xiàn)操作MySQL并執(zhí)行原生sql語句,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06