1. 程式人生 > >python2.7 windows dist,python 打包成exe

python2.7 windows dist,python 打包成exe

python 使用2.7版本,py2exe不能使用最新的版本,最新的依賴python3.3

1. 下載py2exe for python,安裝。
win32位:http://prdownloads.sourceforge.net/py2exe/py2exe-0.6.9.win32-py2.7.exe?download

win64位:http://prdownloads.sourceforge.net/py2exe/py2exe-0.6.9.win64-py2.7.amd64.exe?download

baiduyun share link: http://pan.baidu.com/s/1qW9JmDA


2. 在python專案目錄下,編輯py2exe setup檔案並儲存為setup.py
note: data_files 可以省略不要,可以將一些資源配置檔案一起打包

cat setup.py
# setup.py
from distutils.core import setup
import glob
import py2exe

setup(console=[{
'script":myscript.py",
"icon_resources": [(1, "main.ico")]
}],
      data_files=[("bitmaps",
                   ["bm/large.gif", "bm/small.gif"]),
                  ("fonts",
                   glob.glob("fonts\\*.fnt"))],
)
# This would create a subdirectory dist\bitmaps, containing the two .gif files, and a subdirectory dist\fonts, containing all the .fnt files.

3. 執行命令python setup.py py2exe生成exe, 在dist目錄下就是生成好的獨立可執行的exe檔案
如果想要生成更加精簡的目錄結構,可以指定--bundle引數[1-3],
例如:python setup.py py2exe --bundle 1

補充一下,如果要把python編譯的所有檔案打包到一個exe中,就需要在setup()這個函式中,要設定2個引數:options中的bundle_files和zipfile。
其中bundle_files有效值為:
3 (預設)不打包。
2 打包,但不打包Python直譯器。
1 打包,包括Python直譯器。
zipfile的有效值為:
不填(預設) 生成一個library.zip檔案
None 把所有東西打包進.exe檔案中
例項如下:
#! /usr/bin/env python  
# -*- coding: utf-8 -*-  
#安裝成windows服務的python指令碼  
#內容:  
from distutils.core import setup  
import py2exe  
options = {"py2exe":{"compressed": 1, #壓縮  
                     "optimize": 2,  
                     "bundle_files": 1 #所有檔案打包成一個exe檔案  
                     }}  
setup(  
    service=["PyWindowsService"],  
    options=options,  
    zipfile=None)

更加完整的解決方案:
http://blog.csdn.net/dyx1024/article/details/7417610
http://www.iteye.com/topic/800720