The Python script package into exe executable file

  cx_freeze is used to package Python script into an executable program tool that supports the latest version of Python3.2. The system generates executable file with a cross-platform, and runs no need to install Python. There is currently a similar function tools py2exe and PyInstaller, which looks like py2exe maximum visibility, but has not been updated for a long time, do not comment as to the quality of packaging, after all, turnip greens all have love; PyInstaller do not understand, is said to process very complex; as for cx_freeze the power and ease of use, I strongly recommend.

 

Detail steps are as follows:

  1. Install cx_freeze (official download address: http://cx-freeze.sourceforge.net )

  2. Check cx_freeze installation was successful (Windows OS)

  

  3. Prepare a simple applet hello.py

hello.py
import time

print ("Hello World!")

time.sleep(5)

  4. The Python script packaged into an executable file (two ways)

  •   Use parameters:

  CMD> cxfreeze hello.py --target-dir dist

  • Using a configuration file (personal recommendation => write once, available everywhere ☺):

  CMD> python setup.py build

  setup.py configuration program:

setup.py
 1 #
2 # 文 件 名:setup.py
3 # 功能描述:cx_freeze封装Python脚本的配置文件
4 #
5 # 作者:Renzo 日期:2012/01/01
6 #
7 # 版权:可以使用、传播,但请保留出处;如需修改,请告知作者。
8 #
9
10 from cx_Freeze import setup, Executable
11
12
13 # 首先处理path,includes,excludes,packages等内部变量
14 base = "Win32GUI"
15 path = []
16 includes = []
17 excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
18 'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants',
19 'Tkinter']
20 packages = []
21
22
23 # 这里可以编写客户化的封装前处理代码。例如:数据文件的处理
24
25
26
27 # 配置封装的参数
28 GUI2Exe_Target_Main = Executable(
29 path = path,
30 base = base,
31
32 # 生成可执行文件的主文件
33 script = "simple.py",
34
35 # 生成可执行文件及一些依赖文件的目录
36 targetDir = r"dist",
37 # 可执行文件的名称
38 targetName = "simple.exe",
39 # 可执行文件的ico图标
40 icon = "simple.ico",
41
42 includes = includes,
43 excludes = excludes,
44 packages = packages,
45
46 # 是否需要压缩模块的字节码
47 compress = True,
48
49 # 是否拷贝依赖文件到目标目录
50 copyDependentFiles = True,
51
52 # 是否附加脚本模块到执行文件
53 appendScriptToExe = True,
54 # 是否添加脚本模块到共享库
55 appendScriptToLibrary = False,
56
57 # 设置快捷方式的路径及名称
58 shortcutDir = "",
59 shortcutName = ""
60 )
61
62
63 # 设置安装时软件包的描述信息
64 setup(
65 name = "Simple",
66 version = "0.1",
67 description = "My first python program",
68
69 author = "Renzo",
70 author_email = "[email protected]",
71
72 url = "wwww.cnblogs.com/renzo",
73
74 # 生成的可执行文件
75 executables = [GUI2Exe_Target_Main]
76 )
77
78
79 # 这里可以编写客户化的封装后处理代码。例如:临时数据的清除,数据包的发布等
80
81
82
83 # 到此,整个setup脚本已经完成。
  5. 生成的可执行文件(xxxx.exe)

  6. 执行结果

 

  恭喜你,可以把整个目标目录打包发布了。

转载于:https://www.cnblogs.com/renzo/archive/2012/01/01/2309260.html

Guess you like

Origin blog.csdn.net/weixin_33762130/article/details/94250179