首先要安裝 py2exe 0.5.3,我們舉 dirtozip.py 這個 script 為例::
  #!/usr/bin/env python
  # -*- coding: UTF-8 -*-
  ########################################################################
  #
  """Project ``dirtozip''.
  $Id: dirtozip.py,v 1.1 2004/09/23 22:25:41 yungyuc Exp $
  自動展開 .rar, .zip 或目錄內的圖檔,再壓縮到 .zip 中。  
  由陳永昱 (yyc@seety.org) 開發測試
  copyright 2004, All Rights Reserved.
  """
  #
  ########################################################################  
  ########################################################################
  #
  import sys, os
  import string, re
  from optparse import OptionParser, OptionGroup
  from glob import glob
  from tempfile import mkdtemp
  from zipfile import ZipFile, ZIP_STORED
  #
  ########################################################################  
  def archive( wdir, afn, \
               exts=[ '[jJ][pP][gG]', \
                      '[jJ][pP][eE]', \
                      '[tT][iI][fF]', \
                      '[bB][mM][pP]', \
                      '[pP][nN][gG]', \
                      '[gG][iI][fF]' ] ):
  ########################################################################
  #
    zf = ZipFile( afn, 'w', ZIP_STORED )  
    olddir = os.getcwd()
    os.chdir( wdir )    
    fns = []
    for ext in exts:
      fns.extend( glob('*.'+ext) )
    fns.sort()
    for fn in fns:
      sys.stdout.write( "[%s] <- %s\n" % (afn, fn) )
      zf.write( fn )
    sys.stdout.write( "[%s] done.\n" % afn )
    zf.close()  
    os.chdir( olddir )
  #
  ########################################################################  
  def archive_all( containerdir, repositorydir="." ):
  ########################################################################
  #
    if os.path.exists( repositorydir ):
      if not os.path.isdir( repositorydir ):
        sys.stderr.write( "%s is not a directory, archive stopped.\n" % \
                          repositorydir )
        return 
    else:
      os.makedirs( repositorydir )  
    items = glob( os.path.join(containerdir, "*") )
    for item in items:
      if os.path.isdir(item):
        archive( item, \
                 os.path.join( repositorydir, \
                               os.path.basename(item)+".zip" ) \
               )
        sys.stdout.write("\n")
  #
  ########################################################################  
  def parameters():
  ########################################################################
  #
    op = OptionParser( \
          usage="usage: %prog -f 
 -d  -a", \
      version="%prog, $Revision: 1.1 $" )
  
    opg = OptionGroup( op, "General Specifier" )
    opg.add_option( "-f", "--filename", action="store", type="string", \
                    dest="afn", default="", \
                    help="Archive File Name." )
    opg.add_option( "-d", "--directory", action="store", type="string", \
                    dest="dir", default="", \
                    help="Directory to save." )
    opg.add_option( "-a", "--all", action="store_true", \
                    dest="all", default=False, \
                    help="Archive all directory specified by the -d option, "
                         "and use the place that -f option specified "
                         "to save the archive file(s).")
    op.add_option_group( opg )
    
    (options, args) = op.parse_args()
  
    return options, args, op
  #
  ########################################################################
  
  def main():
  ########################################################################
  #
    # 解析命令列參數
    options, args, op = parameters()
  
    if options.dir != "":
      if options.all:
        if options.afn != "":
          archive_all( options.dir, options.afn )
        else:
          archive_all( options.dir )
      else:
        if options.afn != "":
          archive( options.dir, options.afn )
        else:
          archive( options.dir, os.path.basename(options.dir)+".zip" )
    else:
      op.print_help()
      sys.exit()
  #
  ########################################################################
  
  if __name__ == '__main__':
  ########################################################################
  #
    main()
  #
  ########################################################################
  
  ########################################################################
  #
  # $Log: dirtozip.py,v $
  # Revision 1.1  2004/09/23 22:25:41  yungyuc
  # 經過實用測試,已可用來進行工作。
  #
  ########################################################################
  
用來建立 exe 檔的 dirtozip_setup.py 為::
  #!/usr/bin/env python
  # -*- coding: UTF-8 -*-
  
  """file revision: $Revision$
  """
  
  ########################################################################
  #
  from distutils.core import setup
  import py2exe
  #
  ########################################################################
  
  setup( name = 'dirtozip',
         version = '0.1',
         description = 'Convert Images in Dir into a Zipfile',
         author = 'Yung-Yu Chen', 
         author_email = 'yyc@seety.org', 
         py_modules = ['dirtozip'], 
         console = ['dirtozip.py'] )
  
  # $Log$
  #
  
準備好了這兩個檔案之後,把它們放在一起,然後在 CLI 下::
  python dirtozip_setup py2exe
產生好的 .exe 檔和相關的 .dll 與程式庫,就會出現在 dist 目錄裡面了。