py2exe

 

 

* py2exe is a Python Distutils extension
* converts Python scripts into executable Windows programs

Home Page: http://www.py2exe.org/
Download URL: http://sourceforge.net/project/showfiles.php?group_id=15583

 

 


Debug

 

How does py2exe decide which modules you need

To display this debugging trace, run the py2exe "module finder" code standalone

with at least one -d option to turn on "debugging":

python -m py2exe.mf -d path/to/my_file.py
 


files (py2exe 透過 python 的modulefinder 來找到所需的module)

 

  • myprog.exe        The actual executable.
  • python??.dll       the python interpreter library. This is the brain of your executable   <-- 2.4MByte !!
  • library.zip          pure source modules will be inserted
  • *.pyd                The pyd files are actually standard Windows DLL
  • *.dll                  some pyd probably have some DLL dependencies, and here they come

   
   


setup.py

 

from distutils.core import setup
import py2exe

setup(console=['hello.py'])

# Providing the Microsoft Visual C runtime DLL

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en

當 Client 沒有要 MVC runtime 時, 會見到

"The system cannot execute the specified program."

python.exe setup.py py2exe

Output:

running py2exe
creating D:\py_script\ApacheCheck\build
creating D:\py_script\ApacheCheck\build\bdist.win32
creating D:\py_script\ApacheCheck\build\bdist.win32\winexe
creating D:\py_script\ApacheCheck\build\bdist.win32\winexe\collect-2.7
creating D:\py_script\ApacheCheck\build\bdist.win32\winexe\bundle-2.7
creating D:\py_script\ApacheCheck\build\bdist.win32\winexe\temp
creating D:\py_script\ApacheCheck\dist
*** searching for required modules ***
*** parsing results ***
creating python loader for extension 'win32evtlog'
 (C:\Python27\lib\site-packages\win32\win32evtlog.pyd -> win32evtlog.pyd)
.......................

 


Resource

 

data_files

The data_files takes a tuple

1) the location to store the data and

2) the location to copy the data from

setup(windows=["HtmlConv.py"],
      data_files=[("bmp",["bmp/logo.bmp", "bmp/title.gif"]),
                  ("wav", glob.glob("sounds*.wav"))])

 


sys.executable

 

sys.executable is set to the full pathname of the exe-file.
    The first item in sys.argv is the full pathname of the executable, the rest are the command line arguments.
    sys.frozen only exists in the executable. It is set to "console_exe" for a console executable, to "windows_exe" for a console-less gui executable, and to "dll" for a inprocess dll server.
    __file__ is not defined (you might want to use sys.argv[0] instead)

    __file__ is not defined (you might want to use sys.argv[0] instead)

import os, sys

if hasattr(sys, 'frozen'):
    basis = sys.executable
else:
    basis = sys.argv[0]
required_folder = os.path.split(basis)[0]

 

 


optimize

 

# compiling of the needed modules
setup(
        options={"py2exe":{"optimize":2}},
        console=["beep.py"]
)

# optimized bytecode

python -OO setup.py py2exe

Exclude _ssl

    setup(
        console=['ApacheCheck.py'],
        options={
            "py2exe":{"optimize": 2,"excludes": ['_ssl']}
            }
        )

 


為 EXE 加入 Icons

 

# The resource number does not seem to matter. Windows just takes the first existing icon.
# LiquidIcon is a freeware icon editor that lets you combine multiple ico files of different sizes and bit-depths into a single ico file.

setup.py:

setup(
    windows = [
        {
            "script": "with_gui.py",
            "icon_resources": [(1, "myicon.ico")]
        }
    ],
)

 


Distutils setup keywords

 

console
list of scripts to convert into console exes

windows
list of scripts to convert into GUI exes

service
list of module names containing win32 service classes

# distutils

>>> from distutils.core import setup
>>> help(setup)

 

The options dictionary of py2exe

optimize                (0, 1, or 2) 0 = don’t optimize
compressed              (boolean) create a compressed zipfile
                        (For the price of running a tad slower, you can compress the library zip using the compress option. )
excludes                list of module names to exclude

 

# py2exe automatically includes the entire encodings library in the distribution.
# If you're absolutely sure you won't need to deal with Unicode

"ascii":True

Example:

setup(
        windows=['trypyglet.py'],
        options={
                "py2exe":{
                        "unbuffered": True,
                        "optimize": 2,
                        "excludes": ["email"]
                }
        }
)

 


 

 

Creative Commons license icon Creative Commons license icon