python程式編譯成exe檔案
阿新 • • 發佈:2019-10-19
最近越來越喜歡使用python寫工具。使用的時候,發現程式內部成員python安裝目錄常常不同,如果用bat雙擊執行,常常需要修改從svn上down下來的bat檔案中python.exe的路徑。而給策劃、美術或者QA使用,更是需要讓他們安裝python和各種外掛,他們能把你煩死。所以,必須把py檔案轉成exe檔案,然後做到傻瓜式的雙擊執行。
我瞭解到有兩個常用的套件:py2exe和pyinstaller,我選用的是pyinstaller。這裡記錄一下使用方法。
一、首先,上官網下載,http://www.pyinstaller.org/,推薦32位2.7版本的,然後解壓。
二、使用方法
doc/Manual.html裡寫的很詳細,這裡簡單說一下最基本、常用的功能。
1. cmd下進入解壓的pyinstaller目錄,執行命令格式如下:
python pyinstaller.py [opts] program.py
幾個常用選項包括:
-D, --onedir 建立一個目錄,包含exe檔案和依賴檔案,這是預設選項。(Create a folder name containing an executable name and all support files. This is the default.)
-F, --onefile 建立一個exe檔案,所有依賴檔案都打包進了這個exe檔案,這個exe會比較大,但是我覺得方便使用。(Create a single executable file name (or name .exe
or name .app).)
-c, --console, --nowindowed 控制檯,無介面,預設選項。(Set up a console subsystem for standard input/output at run time. This is the default for both one-file and
one-folder modes.)
-w, --windowed, --noconsole 視窗無控制檯。(On Windows and Mac OS X, do not create a console window at run time for standard input/output. (This option is
ignored for other operating systems.) On Mac OS X, this option triggers the creation of an OS X application bundle.)
比如:D:\soft\python\PyInstaller-2.1>python pyinstaller.py -c -F E:\work\unity\xxx\trunk\tools\excel2json\excel2json.py
報錯,依賴pywin32
2. 安裝pywin32。http://sourceforge.net/projects/pywin32/files/pywin32/,下載後雙擊安裝即可。
3. 再次cmd下執行,D:\soft\python\PyInstaller-2.1>python pyinstaller.py -c -F E:\work\unity\xxx\trunk\tools\excel2json\excel2json.py。
成功生