python程序編譯成exe文件

  • 2019 年 10 月 18 日
  • 筆記

最近越來越喜歡使用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:softpythonPyInstaller-2.1>python pyinstaller.py -c -F E:workunityxxxtrunktoolsexcel2jsonexcel2json.py     報錯,依賴pywin32     2. 安裝pywin32。http://sourceforge.net/projects/pywin32/files/pywin32/,下載後雙擊安裝即可。     3. 再次cmd下執行,D:softpythonPyInstaller-2.1>python pyinstaller.py -c -F E:workunityxxxtrunktoolsexcel2jsonexcel2json.py。                  成功生成exe!