python打生成exe文件

1.   在項目工作目錄下建一個 setup.py 文件

2.   引用python頭文件


       from distutils.core import  setup
       from glob import glob


3.  如果項目中引用了win32com,則需要導入這些引用的模塊,幾乎所有的python項目生成exe這部分代碼都是一樣的,可以通用,導入代碼如下:

     try:
    # py2exe 0.6.4 introduced a replacement modulefinder.
    # This means we have to add package paths there, not to the built-in
    # one.  If this new modulefinder gets integrated into Python, then
    # we might be able to revert this some day.
    # if this doesn't work, try import modulefinder
    try:
        import py2exe.mf as modulefinder
    except ImportError:
        import modulefinder
    import win32com, sys

    for p in win32com.__path__[1:]:
        modulefinder.AddPackagePath("win32com", p)
    for extra in ["win32com.shell"]: #,"win32com.mapi"
        __import__(extra)
        m = sys.modules[extra]
        for p in m.__path__[1:]:
            modulefinder.AddPackagePath(extra, p)
except ImportError:
    # no build path setup, no worries.
    pass


4.    聲明setup函數:

        data_files = [("Microsoft.VC90.CRT",
               glob(r'D:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]


        manifest = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <noInheritable></noInheritable>
    <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.30729.1" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b">
    </assemblyIdentity>
    <file name="msvcr90.dll" hashalg="SHA1" hash="9785b1c493deb5b2134dc4aef3719cee207001bc">
    <asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
    <dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity">
    </dsig:Transform></dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1">
    </dsig:DigestMethod><dsig:DigestValue>VF5ECUAHPV7EnUf+/UIXMPizPvs=</dsig:DigestValue></asmv2:hash>
    </file>
    <file name="msvcp90.dll" hashalg="SHA1" hash="0f6bbf7fe4fb3fca2cb5b542eca1a1cad051f01c">
    <asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
    <dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity"></dsig:Transform>
    </dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1">
    </dsig:DigestMethod><dsig:DigestValue>3Wg+StVMq2uhx7POnAkl2w4dDmY=</dsig:DigestValue></asmv2:hash>
    </file>
    <file name="msvcm90.dll" hashalg="SHA1" hash="7f3290ab2b7444c2b4a9b1fedfdb16466d7a21bb">
    <asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
    <dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity">
    </dsig:Transform>
    </dsig:Transforms>
    <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1">
    </dsig:DigestMethod><dsig:DigestValue>/YfRn7UQENzdMeoMHxTgdRMiObA=</dsig:DigestValue></asmv2:hash>
    </file>
    <description>ETimes SafeDocument </description>
    <dependency>
        <dependentAssembly>
          <assemblyIdentity type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*' />
        </dependentAssembly>
  </dependency>
</assembly>
"""


        py2exe_options = {"py2exe": {
    "compressed": 1,
    "optimize": 0,
    "bundle_files": 1,
    "dll_excludes": ["mswsock.dll", "powrprof.dll"],
    "excludes": ["email", "System", "clr"],
    "typelibs": [("{A435DD76-804E-4475-8FAB-986EACD1C6BE}", 0x0, 1, 0), ]
}
}


       setup(
    data_files=data_files,
    windows=[{
        'script': 'client.pyw',
        "other_resources":[(24,1,manifest)],
        "icon_resources": [
            (0, './png/Attach.ico')
        ]}],
    zipfile='core.lib',
    options=py2exe_options
)

      參數作用:

          data_files :  數據”文件,你會需要您的可執行文件運行諸如列表的PNG,JPG格式。

          script:   項目中的主程序,一般是主界面所在的.py文件中

          icon_resources:項目中用到的圖標

          other_resources: 就用上面的

           zipfile: 包的名字

           options參數:

                      dll_excludes: 不打包進來的dll,一般是系統自帶有的

                      typelibs:項目中用到的插件/com組件的clsid


5.    打開命令行窗口

        進入到項目根目錄下,運行   python setup.py py2exe,會在根目錄下新建dist和build文件夾,exe文件就在dist文件夾中,雙擊exe文件就可運行程序了




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