使用Pyinstaller轉換.py文件為.exe可執行程序


    pyinstaller能夠在Windows、Linux等操作系統下將Python腳本打包成可直接運行程序。使Python腳本可以在沒有安裝Python的環境中直接運行,方便共享。

開發環境

  python 2.7.12 + Windows7

注意事項

   1、待轉換的.py文件絕對路徑最好不要包含中文字符。容易出現一些莫名其妙的問題。

    2、python中需要有.py文件中用到的第三方庫。否則在轉換后的.exe文件中會出現不符合預期的結果。

pyinstaller安裝步驟

  1、配置pip鏡像源。pip配置方法參考pip配置和安裝第三方模塊。如果已經配置,跳過。

  2、打開cmd命令行窗口,輸入pip install pyinstaller,安裝pyinstaller庫。

C:\Users\Administrator>pip install pyinstaller Collecting pyinstaller Downloading http://pypi.doubanio.com/packages/3c/86/909a8c35c5471919b3854c01f43843d9b5aed0e9948b63e560010f7f3429/PyIns taller-3.3.1.tar.gz (3.5MB) 100% |████████████████████████████████| 3.5MB 112kB/s Requirement already satisfied: setuptools in c:\python27\lib\site-packages (from pyinstaller) Requirement already satisfied: pefile>=2017.8.1 in c:\python27\lib\site-packages (from pyinstaller) Requirement already satisfied: macholib>=1.8 in c:\python27\lib\site-packages (from pyinstaller) Requirement already satisfied: dis3 in c:\python27\lib\site-packages (from pyinstaller) Requirement already satisfied: future in c:\python27\lib\site-packages (from pyinstaller) Requirement already satisfied: altgraph>=0.15 in c:\python27\lib\site-packages (from macholib>=1.8->pyinstaller) Installing collected packages: pyinstaller Running setup.py install for pyinstaller ... done Successfully installed pyinstaller-3.3.1

  3、確認pyinstaller安裝結果,位於c:\Python27\Scripts路徑下。執行where pyinstaller查看

C:\Users>where pyinstaller c:\Python27\Scripts\pyinstaller.exe

pyinstaller基本語法

  pyinstaller [options] script

  options常用選項說明:

-F,-onefile: 表示生成單個可執行文件,常用。 -w, -windowed, -noconsole:表示去掉控制台窗口,這在GUI界面時非常有用。不過如果是命令行程序的話那就把這個選項刪除吧! -p 表示你自己自定義需要加載的類路徑,一般情況下用不到 -i 表示可執行文件的圖標。注意:圖片后綴必須是.ico
-c,console,-nowindowed:使用控制台,無窗口(默認)
-D,-onedir:創建一個目錄,包含EXE文件,但會依賴很多文件(默認選項)

  基本實例:pyinstaller -F myscript.py。

  pyinstaller更多語法見官網說明:https://pyinstaller.readthedocs.io/en/stable/usage.html

pyinstaller原理簡介

  pyinstaller其實就是把python解釋器和腳本打包成一個可執行文件,和編譯成真正的機器碼是完全兩回事。所以打包不一定會提高運行效率,可能會降低運行效率,但是好處是在運行者機器上不用安裝python和腳本所依賴的庫。

  輸入指定的腳本后,首先pyinstaller會分析該腳本所依賴的其他依賴,然后進行查找、復制,把所有相關的依賴都收集起來並驚醒加密處理,包括python解釋器,最后把這些文件放在一個目錄下,或者打包到一個可執行文件。然后就可以直接運行所生成的可執行文件。

  需要注意的是,使用pyinstaller打包生成的可執行文件,只能再和打包機器系統相同的環境下運行。32位python環境打包的程序可以運行在32/64位windows系統上。64位python環境打包的程序只能運行在64位windows系統上。所以如果想打包程序的話,建議使用32位python環境打包。

pyinstaller使用實例

1、確認待轉換的.py文件可正確運行,不存在語法錯誤。如ccc.py

2、執行pyinstaller -F ${Python腳本名}完成文件轉換。.exe文件生成的絕對路徑會在倒數第二行顯示,通常位於當前目錄下dist所在目錄下。轉換后的.exe文件名與python文件名相同。如下圖所示

d:\Program Files\Notepad++>pyinstaller -F ccc.py 213 INFO: PyInstaller: 3.3.1
226 INFO: Python: 2.7.12
237 INFO: Platform: Windows-7-6.1.7601-SP1 ....... 8136 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 4940) 10315 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully. 10341 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe 10355 INFO: checking EXE 10369 INFO: Building EXE because out00-EXE.toc is non existent 10386 INFO: Building EXE from out00-EXE.toc 10401 INFO: Appending archive to EXE d:\Program Files\Notepad++\dist\ccc.exe 10432 INFO: Building EXE from out00-EXE.toc completed successfully.

FAQs

  1、如果Python腳本使用到了第三方庫,如何打包?

     方法一:將第三方庫對應的包復制到待打包python腳本的同目錄下,再執行打包命令。

     方法二:pyinstaller.exe -F  路徑\文件名.py 路徑\文件名.py 

  2、我的python腳本主要是命令行輸出,但是程序執行完就退出無法查看相關信息,如何處理?

      在python腳本最后一行添加命令:os.system('pause') 或者 raw_input('Press enter any key to exit...')

  3、 我想給我的打包后的執行程序換個圖標,如何處理?

  使用參數-i。如命令:pyinstaller -F -i tupian\qq.ico ccc.py。文件后綴名必須是.ico

  4、程序運行出現CMD窗口,如何去除?

  帶上參數-w即可。pyinstaller.exe -F call_login.py -w (-w表示去掉控制台窗口顯示)


免責聲明!

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



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