将Python脚本程序发布成一个exe可执行文件

目的

Python脚本如果需要发布给Windows客户运行,可以使用py2exe来编译成exe可执行文件, 这样用户就不用安装Python运行环境。 顺便还保护了源代码。(不过要注意,还是可以反编译的)

下载及安装

首先要确认本地安装的python的版本,然后选择Py2exe版本。

到目前为止似乎还不支持Python3的脚本。 还好我一般不用Python3,因为多数Linux系统自带的都是2.7版本的。

使用方法

例如需要编译的脚本为:hello.py

'''
Created on 2012-9-20
@author: tudaxia
'''
#!/usr/bin/env python
# coding:utf-8

def HelloWorld():
    print "Hello World!"

if __name__=="__main__":
    HelloWorld()

编写一个编译脚本,例如hello_setup.py

'''
Created on 2012-9-20
@author: tudaxia
'''
#!/usr/bin/env python
# coding:utf-8
from distutils.core import setup
import py2exe  

includes = ["encodings", "encodings.*"]  

options = {"py2exe":
            {"compressed": 1,
             "optimize": 2,
             "ascii": 1,
             "includes":includes,
             "bundle_files": 1 # 表示将所有的模块打包成一个exe文件
            }}
setup(
    options=options,
    zipfile=None,
    console=[{"script": "hello.py", "icon_resources": [(1, "tudaxia.ico")]}],  #可以配置自己的图标
    version = "2012.09.20",   # 按实际情况
    description = "Hello World!", # 按实际情况
    name = "hello", # 按实际情况
)

在命令行下,执行以下命令

d:\demo\hello_setup.py py2exe

在当前目录下会生成一个dist目录,里面有hello.exe可执行文件。 这样hello.exe文件就可以脱离python环境(其实是自带了一个)独立运行了。

编译options参数有很多,可以执行命令查看帮助:

D:\demo>hello_setup.py py2exe --help
Common commands: (see '--help-commands' for more)

  setup.py build      will build the package underneath 'build/'
  setup.py install    will install the package

Global options:
  --verbose (-v)  run verbosely (default)
  --quiet (-q)    run quietly (turns verbosity off)
  --dry-run (-n)  don't actually do anything
  --help (-h)     show detailed help message
  --no-user-cfg   ignore pydistutils.cfg in your home directory

Options for 'py2exe' command:
  --optimize (-O)       optimization level: -O1 for "python -O", -O2 for
                        "python -OO", and -O0 to disable [default: -O0]
  --dist-dir (-d)       directory to put final built distributions in (default
                        is dist)
  --excludes (-e)       comma-separated list of modules to exclude
  --dll-excludes        comma-separated list of DLLs to exclude
  --ignores             comma-separated list of modules to ignore if they are
                        not found
  --includes (-i)       comma-separated list of modules to include
  --packages (-p)       comma-separated list of packages to include
  --compressed (-c)     create a compressed zipfile
  --xref (-x)           create and show a module cross reference
  --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels
                        are 1, 2, or 3 (default)
  --skip-archive        do not place Python bytecode files in an archive, put
                        them directly in the file system
  --ascii (-a)          do not automatically include encodings and codecs
  --custom-boot-script  Python file that will be run when setting up the
                        runtime environment

usage: hello_setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: hello_setup.py --help [cmd1 cmd2 ...]
   or: hello_setup.py --help-commands
   or: hello_setup.py cmd --help
Share Comments
comments powered by Disqus