如何把Python脚本导出为exe程序
轉(zhuǎn)載自:?http://www.jianshu.com/p/8dbdfbd3716d
?
一.pyinstaller簡(jiǎn)介
pyinstaller將Python腳本打包成可執(zhí)行程序,使在沒(méi)有Python環(huán)境的機(jī)器上運(yùn)行
最新版是pyinstaller 3.1.1。支持python2.7和python3.3+。
可運(yùn)行在Windows,Mac和Linux操作系統(tǒng)下。
但它不是跨編譯的,也就是說(shuō)在Windows下用PyInstaller生成的exe只能運(yùn)行在Windows下,在Linux下生成的只能運(yùn)行在Linux下。
二.pyinstaller在windows下的安裝
使用命令pip install pyinstaller即可
在windows下,pyinstaller需要PyWin32的支持。當(dāng)用pip安裝pyinstaller時(shí)未找到PyWin32,會(huì)自動(dòng)安裝pypiwin32
出現(xiàn)Successfully installed pyinstaller-3.1.1 pypiwin32-219即表示安裝成功
三.打包
打包的app里并不包含任何源碼,但將腳本的.pyc文件打包了。
基本語(yǔ)法:
pyinstaller options myscript.py
常用的可選參數(shù)如下:
--onefile 將結(jié)果打包成一個(gè)可執(zhí)行文件
--onedir 將所有結(jié)果打包到一個(gè)文件夾中,該文件夾包括一個(gè)可執(zhí)行文件和可執(zhí)行文件執(zhí)行時(shí)需要的依賴(lài)文件(默認(rèn))
--paths=DIR 設(shè)置導(dǎo)入路徑
--distpath=DIR 設(shè)置將打包的結(jié)果文件放置的路徑
--specpath=DIR 設(shè)置將spec文件放置的路徑
--windowed 使用windows子系統(tǒng)執(zhí)行,不會(huì)打開(kāi)命令行(只對(duì)windows有效)
--nowindowed 使用控制臺(tái)子系統(tǒng)執(zhí)行(默認(rèn))(只對(duì)windows有效)
--icon=<FILE.ICO> 將file.ico添加為可執(zhí)行文件的資源(只對(duì)windows有效)
如pyinstaller --paths="D:\Queena" guess_exe.py
四.小實(shí)例(windows下)
寫(xiě)好游戲文件guess_exe.py,代碼如下:
__author__ = 'qa-2' # -*- coding:utf-8 -*- # 搖3次骰子,當(dāng)總數(shù)total,3<=total<=10時(shí)為小,11<=total<=18為大 import random import timedef 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 * ratewhile 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 * ratereturn stake,ratedef roll_dice(times = 3):'''搖骰子'''print('<<<<<<<<<< Roll The Dice! >>>>>>>>>>')points_list = []while times > 0:number = random.randrange(1,7)points_list.append(number)times -= 1return points_listdef roll_result(total):'''判斷是大是小'''is_big = 11 <= total <= 18is_small = 3 <= total <= 10if is_small:return 'Small'elif is_big:return 'Big'def settlement(boo,points_list,current_money,stake = 1000,rate = 1):'''結(jié)余'''increase = stake * rateif boo:current_money += increaseprint('The points are ' + str(points_list) + ' .You win!')print('You gained $' + str(increase) + '.You have $' + str(current_money) + ' now.' )else:current_money -= increaseprint('The points are ' + str(points_list) + ' .You lose!')print('You lost $' + str(increase) + '.You have $' + str(current_money) + ' now.' )return current_moneydef sleep_second(seconds=1):'''休眠'''time.sleep(seconds)# 開(kāi)始游戲 def start_game():'''開(kāi)始猜大小的游戲'''current_money = 1000print('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_resultcurrent_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?就會(huì)在當(dāng)前文件下形成build文件夾、dist文件夾和.spec文件。
dist里就是guess_exe.exe可執(zhí)行文件。
spec文件告訴PyInstaller如何去處理腳本。它對(duì)腳本名以及大多數(shù)pyinstaller的可選參數(shù)進(jìn)行加密。PyInstaller就是通過(guò)執(zhí)行spec文件的內(nèi)容來(lái)build the app。
五. 參加麻瓜編程心得:
我最大的感想是魔力教程的優(yōu)美,它清晰、簡(jiǎn)潔、易懂。
視頻學(xué)習(xí)過(guò)程中深刻體會(huì)到了編制者的用心,精美的配圖加上適宜的背景音樂(lè),基礎(chǔ)知識(shí)和貼切的小項(xiàng)目,這一系列的配套成就了麻瓜的不凡。
成功的學(xué)會(huì)十萬(wàn)數(shù)據(jù)的爬取之后,那種成就感簡(jiǎn)直了哎呀,無(wú)法言喻。之后是數(shù)據(jù)的可視化還有各種圖形以及顯示在網(wǎng)頁(yè)上,這一連串的成就都讓我很開(kāi)心,而且這個(gè)技能讓我在職業(yè)技術(shù)上有了很大的提升,以后跳槽我又有了資本,十分感謝麻瓜!
六. 參考網(wǎng)址:
http://pythonhosted.org/PyInstaller/
http://blog.csdn.net/zc02051126/article/details/8827868
轉(zhuǎn)載于:https://www.cnblogs.com/Dota-wiki/p/7851493.html
總結(jié)
以上是生活随笔為你收集整理的如何把Python脚本导出为exe程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 创建一个方便设计的自定义栅格布局
- 下一篇: Linux多网口绑定配合华为5700 e