使用PyInstaller——Python程序轉換為EXE


PyInstaller可以將Python程序打包成Windows(當然也包括Linux, Mac OS X, Solaris and AIX)下可執行的EXE文件,目前支持python2.2-2.7版本,點擊這里下載。

使用PyInstaller需要安裝PyWin32,可到這里下載相應的版本。(從pywin32的下載量看,還是Python2.7使用更廣泛)

 

下載對應已安裝的Python版本的PyInstaller版本,解壓到任意目錄,按照提供的manual文檔進行即可。假設要轉換的Python代碼為txexe\hello.py.進入PyInstaller安裝根目錄,執行以下命令。

python Configure.py #僅第一次執行
python Makespec.py toexe\hello.py #生成的hello.spec文件存默認放在當前目錄的hello文件夾下
python Build.py hello\hello.spec #生成的exe文件在當前目錄下的hello\dist文件夾下

我們也可以指定Python程序路徑以及輸出文件路徑。

更詳細的解釋如下:

第一步:Configuring your PyInstaller setup

pyinstaller安裝目錄下cmd運行:python Configure.py

基於當前系統配置PyInstaller,如果更換了Python版本,需要重新下載相應版本的PyInstaller並重新執行Configure.py

 注意關注運行過程中的警告和錯誤,最好沒有警告和錯誤,如果出現找不到某dll,可以下載后放到C:\Windows\system32下,一般都能解決。

第二步:Create a spec file for your project

運行:python Makespec.py hello.py

生成要轉換的腳本的spec文件,告訴PyInstaller創建一個包含主要的可執行文件和動態庫。可以通過-o選項指定輸出spec文件的路徑:

Makespec.py的可用參數:

-F--onefile produce a single file deployment (see below).
-D--onedir produce a single directory deployment (default).
-K--tk include TCL/TK in the deployment.
-a--ascii do not include encodings. The default (on Python versions with unicode support) is now to include all encodings.
-d--debug use debug (verbose) versions of the executables.
-w--windowed--noconsole
  Use the Windows subsystem executable, which does not open the console when the program is launched. (Windows only)
-c--nowindowed--console
  Use the console subsystem executable. This is the default. (Windows only)
-s--strip the executable and all shared libraries will be run through strip. Note that cygwin's strip tends to render normal Win32 dlls unusable.
-X--upx if you have UPX installed (detected by Configure), this will use it to compress your executable (and, on Windows, your dlls). See note below.
-o DIR--out=DIR
  create the spec file in directory. If not specified, and the current directory is Installer's root directory, an output subdirectory will be created. Otherwise the current directory is used.
-p DIR--paths=DIR
  set base path for import (like using PYTHONPATH). Multiple directories are allowed, separating them with the path separator (';' under Windows, ':' under Linux), or using this option multiple times.
--icon=<FILE.ICO>
  add file.ico to the executable's resources. (Windows only)
--icon=<FILE.EXE,N>
  add the n-th incon in file.exe to the executable's resources. (Windows only)
-v FILE--version=FILE
  add verfile as a version resource to the executable. (Windows only)
-n NAME--name=NAME
  optional name to assign to the project (from which the spec file name is generated). If omitted, the basename of the (first) script is used.

常用參數:

-F    制作獨立的可執行程序
-D    制作出的檔案存放在同一個文件夾下(默認值)

-K    包含TCL/TK(對於使用了TK的,最好加上這個選項,否則在未安裝TK的電腦上無法運行)
-w     制作窗口程序
-c    制作命令行程序(默認)
-X    制作使用UPX壓縮過的可執行程序(推薦使用這個選項,需要下載UPX包,解壓后upx.exe放在Python(非PyInstaller)安裝目錄下,下載upx308w.zip

-o DIR  指定輸出SPEC文件路徑(這也決定了最后輸出的exe文件路徑)
--icon=[ICO文件路徑] 指定程序圖標
-v [指定文件] 指定程序版本信息
-n [指定程序名] 指定程序名稱

關於SPEC文件細節,點這里


第三步:Build your project

運行:python Build.py hello\hello.spec

使用上一步得到的spec文件Build,產生的文件放在spec當前目錄,有兩個文件夾,build是該步驟建立的工程文件,dist存放最后輸出的exe文件:hello.exe.

 這里還有一個關於PyInstaller的介紹,很不錯:http://bytes.com/topic/python/insights/579554-simple-guide-using-pyinstaller

【完】


免責聲明!

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



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