(轉)使用 PyInstaller 把python程序 .py轉為 .exe 可執行程序


最近使用Python為項目開發一款繪圖工具(繪出 聲場三維模型)。因為希望能把Python腳本發布為脫離Python平台運行的可執行程序,比如單個的exe文件。PyInstaller恰滿足這個需求。本文PyInstaller的版本是2.0,支持Python2.7。下面討論怎樣安裝,使用PyInstaller。PyInstaller本身並不屬於Python包。在安裝 pyinstaller 之前假設你已經安裝了python ,注意把python 環境變量配置好, 即 進入cmd后 輸入 python 會進入 python  shell.

pyinstaller安裝:

1,下載pyinstaller並解壓(可以去官網下載最新版):

2,安裝最新版本的 pywin32-217.win32-py2.7.exe:

     不然會出現錯誤Error: PyInstaller for Python 2.6+ on Windows needs pywin32.

3,進入cmd , 

1 d:\>cd d:\PyInstaller-2.1  
2 d:\pyinstaller>python pyinstaller.py  

Usage: python pyinstaller.py [opts] <scriptname> [ <scriptname> ...] | <specfile>   
pyinstaller.py: error: Requires at least one scriptname file or exactly one .spec-file

以上信息表示可以開展工作了,以下是測試一個demo.py 文件的打包,文件放在當前目錄的pyinstaller-2.1文件夾里面 .

1 d:\pyinstaller-2.1>python pyinstaller.py --console --onefile  demo.py  

上面的命令成功運行后demo.py 會生成一個demo文件夾。在這個文件夾下面會有一個名為dist的文件夾,此文件夾下面有轉換好的demo.exe。

點擊 demo.exe,首先會出來一個 控制台的黑窗口,如下圖:

4, 上面編譯出來的exe能夠正常運行了,但帶一個黑色的console,以下重新編譯,加入--windowed --icon,取消--console   

D:\PyInstaller-2.1>python pyinstaller.py  -w  --onefile --icon="my.ico" demo.py  

icon是你喜歡的圖標文件,py為你要打包的程序。最后生成一個demo.exe可執行文件,點擊demo.exe直接顯示所繪圖形。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM