python編譯成windows exe可執行檔案
pyInstaller是一款用於將pyhon程式打包成exe檔案的工具,pyInstaller不是一個python的包, 只需要把pyInstaller的檔案下載下來放到任意為止都可以,也就是說pyInstaller相當於獨立出來專門幹打包python的工具,這貨是工具不是庫,也不是包…
1.下載安裝pyinstaller,下載網址:http://www.pyinstaller.org/去下載,然後解壓到任意目錄.(注意:在pyinstaller2.0之前的版本要是移動這個資料夾的話,需要改一些配置,不然pyinstaller會因為你的移動不能使用.這個和後面的配置環境有關.pyinstaller2.0的版本就沒有限制.)
2.編譯配置環境(以解壓到F:盤為例子)
①, pyinstaller-2.0的方法:
開啟cmd cd進入解壓後的目錄下的sorce下,即:cd F:pyinstaller-2.0source
執行 python ./waf configure build install
*(可能需要安裝pywin32.下載並安裝即可,重新執行命令即可)
②, pyinstaller-1.5
開啟cmd cd進入pyinstaller解壓目錄,cd F:pyinstaller-1.5
執行 python Configure.py
*(可能需要安裝pywin32.下載並安裝即可,重新執行命令即可)
結果提示儲存在config.dat檔案中。其中config.dat檔案存放在pyinstaller的根目錄。
3. 將自己寫的py程式打包exe
①, pyinstaller-2.0的方法:
將自己寫的py檔案(HelloWorld.py)拷貝到pyinstaller解壓目錄下,
開啟cmd cd進入解壓後的目錄下,即:cd F:pyinstaller-2.0
執行命令 python pyinstaller.py [opts] yourprogram.py
在此處則是執行:python pyinstaller.py [opts] HelloWorld.py
在目錄下會生成一個helloworld的資料夾,裡面存放 HelloWorld.spec檔案,;兩個資料夾(dist,buildi),可以執行的exe則放在/dist/yourprogram下
②, pyinstaller-1.5
將自己寫的py檔案(HelloWorld.py)拷貝到pyinstaller解壓目錄下,
開啟cmd cd進入解壓後的目錄下,即:cd F:pyinstaller-1.5
pyinstaller-1.5和 pyinstaller-2.0 的區別來了。
在 pyinstaller-1.5 中你先需要生成spec檔案,然後通過spec檔案在生成exe檔案,而 pyinstaller-2.0 在執行命令(請看上面)後會同時吧exe和spec檔案都生成出來了
所以在 pyinstaller-1.5 你得先生成spec檔案:
python Makespec.py [opts] yourprogram.py
執行命令: python Makespec.py [opts] HelloWorld.py
此時在此目錄下生成的 HelloWorld資料夾,資料夾下有一個 HelloWorld.spec檔案,將檔案在拷貝到目錄(F:pyinstaller-1.5)下
執行命令: python Build.py HelloWorld.spec
4.你會發現按照上面的方法在/yourprogram/dist/yourprogram下除了需要的exe檔案,也存在很多dll檔案,然後你會開始抱怨作者我坑爹了吧….
哈哈,如果你這麼發現了,說明,你已經完成用最基礎的pyinstaller打包了,關於如何pyinstaller請往下看:
不管是pyinstaller-1.5還是pyinstaller-2.0,要想打包成一個檔案,或者將檔案的圖示修改等等操作,這些都與你在執行命令時有關係,還記得命令是什麼嗎?
pyinstaller-2.0 是:
python pyinstaller.py [opts] yourprogram.py
pyinstaller-1.5 是:
python Makespec.py [opts] yourprogram.py
那些打包成一個檔案,或者將檔案的圖示修改等等操作都和這個 [opts] 有關係.
下面舉些例子(客官一看便知,以 pyinstaller-2.0為例子,1.5通用):
①, 打包成一個檔案:
python pyinstaller.py –onefile yourprogram.py
②,修改exe的圖示(D:BabyWandHsrcfaviconw.ico為圖示的存放路徑):
python pyinstaller.py –onefile –icon=D:BabyWandHsrcfaviconw.ico yourprogram.py
[opts]的一些說明(具體可以看pyinstaller官網http://www.pyinstaller.org/expor … talling-pyinstaller):