用cxfreeze将python转为exe

cxfreeze是一个可以将python脚本转为exe可执行文件的工具,官网网址为:http://cx-freeze.readthedocs.org/en/latest/。在winpython当中有集成,支持python3,而且可以跨平台。

使用方法:

(1)cxfreeze xxx.py --target-dir dist

将xxx.py文件转为exe,目标文件在dist文件夹中,cxfreeze会自动处理依赖关系,将相关的dll文件复制到dist文件夹中。但是,这样产生的exe文件执行时会显示控制台窗口,如果是GUI应用,那么就会显得不大美观,所以可以考虑方法(2)。

(2)通过setup.py文件来进行配置,然后运行下面命令就可以产生exe文件。

python setup.py build

我们可以使用cxfreeze-quickstart来自动产生setup.py文件,但是,我运行这个文件时发生了错误。下面提供一个可以正常运行的例子,将PyQt4提供的一个sample转为exe文件。

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os","re"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "calc",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("calculatorform.pyw", base=base, icon="calculator.ico")])


你可能感兴趣的:(python,windows)