Python3.4(Pyhon)代碼如何打包成.exe可執行文件——詳細教程

剛學Python還沒一個月,迫於急着要搞一個項目的demo演示程序,由於這個demo是有GUI界面,而且代碼裏用了好多模塊,爲了拿出去讓別人演示的時候可以不用再次安裝那麼多的庫包,將Python代碼打包成exe可執行文件還是很有必要的。

我就將我打包過程中研究的東西或遇到的問題在這裏總結一下,儘量寫成一個詳細的教程。

1.可以打包Python代碼的工具

1.1. py2exe:
這個是知名度最高的,但是好像不太適合新手,需要各種庫,用起來比較繁瑣,不推薦啊

1.2. Pyinstaller :
可以只是生成單獨的可執行程序
且支持的版本也多:2.3到2.7都支持。以及x64也支持
也可以自定義圖標
還是挺不錯的啊,我用下面鏈接的教程試着打包過,但沒有成功,原因還在進一步分析中,如果打包成功了就寫一個Pyinstaller的教程。

先在這兒貼上這個Pyinstaller教程,寫的挺不錯的。

【記錄】用PyInstaller把Python代碼打包成單個獨立的exe可執行文件

1.3 cx_Freeze :
這個打包質量挺好,操作也簡單,本人用這個摸索了2個小時,然後就成功打包了 python代碼。缺點是不能生產單獨的可執行文件,這樣子的結果就是exe文件被淹沒在衆多文件中,看着不高大上(另有一說法是可以的,我沒試出來)。

2. 用cx_Freeze打包Python代碼到exe文件

2.1. 首先需要去下載cx_freeze 並安裝

下載地址:http://sourceforge.net/projects/cx-freeze/files/

文檔地址:Welcome to cx_Freeze’s documentation
建議自學能力強的同學直接轉到參考文檔的頁面!!

下載與自己的系統和Python版本對應的安裝包,然後安裝,安裝好後用cmd 命令檢查一下時候安裝正確。
(1) 運行cmd,將路徑切換到你的python安裝路徑下的Scripts文件下,然後執行cxfreeze -h,然後出現下面的圖,就說明安裝正確啦。

這裏寫圖片描述

有新手可能會問那怎麼才能將路徑切換到你的python安裝路徑下的Scripts文件下呢?

下面是CMD 教程:

DOS命令行運行程序:
dir :顯示當前目錄下的文件及文件夾
cd 目錄名:進入當下目錄裏
cd.. : 返回上級目錄
E: :切換到E盤
下圖是實例:

這裏寫圖片描述

言歸正傳,如何使用該命令沒有出現上如所示的畫面,那就要查看一下Python的安裝路徑。

修改路徑的辦法:
我的電腦->左擊鼠標選‘屬性’->選高級系統設置 -> …選擇環境變量,點擊Path,在path的最後面加上自己的安裝路徑,一般是這個樣子的:
這裏寫圖片描述

注意後面加‘;’分號。
再重新運行CMD命令,就好了。

2.2. 安裝Python的擴展包

cx_Freeze擴展包下載地址http://www.lfd.uci.edu/~gohlke/pythonlibs/

這裏寫圖片描述

(1)首先,修改下載文件的擴展名,把擴展名whl,改爲zip
(2) 然後,把該文件解壓縮,取出其中的3個子目錄
(3)其次,刪除cx_freeze的舊包:把Python 安裝目錄下,Lib\site-packages 裏與 cx_Freeze 相關的子目錄刪除
(4) 最後,放置cx_freeze的新包:在 Lib\site-packages 中,放置上述取出的3個子目錄
這個時候差不多就可以開始打包了。

2.3. 打包python代碼生成exe文件

運行cmd,將路徑切換到python的安裝路徑下的Scripts目錄下,輸入命令:

cxfreeze X:\XX|XX.PY - -target-dir yy:\yy\yyy

X:\XX|XX.PY是你要打包的主文件(即啓動文件)的路徑,yy:\yy\yyy是要生成的目標文件夾得路徑,在yyy目錄下就有exe可執行文件。

這裏寫圖片描述

你會看到cmd在運行一小段時間後停止,這是就可以找到目標文件,會驚喜的發現文件夾下面有個.exe文件,點擊運行,就會出來你要的效果了。

這裏寫圖片描述

如果生成的exe不能運行,我在這裏提供幾個查錯方向

(1)你的.py程序中是不是涉及到了對圖片,或者文檔的調用,如果有,那請把這些文件copy到這個exe目標文件下,當然前提是你代碼中對文檔圖片的調用使用的是相對路徑。

(2)查查你的代碼是不是完整的,即在python shell (或別的代碼編輯器)下運行時保證不會出現紅色的ERROR. 具體出錯類型可以查看那個黑色的控制窗口。

2.4 去掉黑色的控制窗

當python代碼全部OK的時候,這個黑色的窗就顯得很多餘,很LOW了。這是我們就要把它去掉。重新生成沒有黑窗的exe,命令如下:

‘cxfreeze X:\XX|XX.PY –target-dir yy:\yy\yyy - -base-name=win32gui’

然後你就可以拿去任何一個電腦上去運行你的python程序了。

2.5 生成單一可執行文件的方法

cx_Freeze默認情況下,是會生成,一個可執行文件,加上一堆運行所需的(.dll或.so等)庫文件,就像上面給出的圖片一樣。

如果想要生成單一的可執行文件:可以使用

distutils setup script:http://cx-freeze.readthedocs.org/en/latest/distutils

由於公司時間急,我還沒有研究過這個,感興趣的可以去看看,好像也不要複雜。

3.cx_Freeze的其他選項參數

授人以魚不如授人以漁,最後這點很關鍵,你可以通過修改其他參數來實現自己想要的功能,最後附上cxfreeze的其他的參數:

cxfreeze其他參數的含義

–version

show version number and exit

-O

optimize generated bytecode as per PYTHONOPTIMIZE; use -OO in order to remove doc strings

-c, –compress

compress byte code in zip files

-s, –silent

suppress all output except warnings and errors

–base-name=NAME

file on which to base the target file; if the name of the file is not an absolute file name, the subdirectory bases (rooted in the directory in which the freezer is found) will be searched for a file matching the name

–init-script=NAME

script which will be executed upon startup; if the name of the file is not an absolute file name, the subdirectory initscripts (rooted in the directory in which the cx_Freeze package is found) will be searched for a file matching the name

–target-dir=DIR, –install-dir=DIR #我們用的就是這一條哦

The directory in which to place the target file and any dependent files

–target-name=NAME

the name of the file to create instead of the base name of the script and the extension of the base binary

–default-path=DIRS

list of paths separated by the standard path separator for the platform which will be used to initialize sys.path prior to running the module finder

–include-path=DIRS

list of paths separated by the standard path separator for the platform which will be used to modify sys.path prior to running the module finder

–replace-paths=DIRECTIVES

replace all the paths in modules found in the given paths with the given replacement string; multiple values are separated by the standard path separator and each value is of the form path=replacement_string; path can be * which means all paths not already specified

–include-modules=NAMES

comma separated list of modules to include

–exclude-modules=NAMES

comma separated list of modules to exclude

–ext-list-file=NAME

name of file in which to place the list of dependent files which were copied into the target directory

-z SPEC, –zip-include=SPEC

name of file to add to the zip file or a specification of the form name=arcname which will specify the archive name to use; multiple –zip-include arguments can be used

–icon=ICON

name of the icon file for the application

結束語:
如果大家按我的方法操作還不能實現Python代碼打包的話,下面我給出幾個鏈接,可以繼續參考:

【記錄】用cx_Freeze把Python代碼打包成單個獨立的exe可執行文件

利用cx_Freeze將py文件打包成exe文件(圖文全解)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章