cx_Freeze将py文件打包成exe文件,隐藏控制台

来源:互联网 发布:华硕mg248q设置软件 编辑:程序博客网 时间:2024/06/05 04:49

docs:http://cx-freeze.readthedocs.io/en/latest/distutils.html

download:https://pypi.python.org/pypi/cx_Freeze/

在python中比较常用的python转exe方法有三种,分别是cx_freeze,py2exe,PyInstaller。py2exe恐怕是三者里面知名度最高的一个,但是同时相对来说它的打包质量恐怕也是最差的一个。pyinstaller打包很好,但是操作工序较为复杂。

cx_freeze打包的exe需要和依赖库放在一个文件夹

首先:cx_freeze下载地址: http://sourceforge.net/projects/cx-freeze/files/ 

第一步:检查cx_freeze是否安装正确。

进入Python的Scripts文件夹

>cxfreeze -h

显示用法,则安装正确

第二步:如果安装正确,那么接下来的事情就非常简单了

正式开始打包,命令为:
cxfreeze hello.py --target-dir dist
加上红色参数可以隐藏控制台
cxfreeze hello.py   --base-name="win32gui"

命令解释:hello.py 是你要打包的主文件、启动文件

Dist为要目标文件夹,打包后会生成dist目录,里面就有打包后的可执行文件。

注意:

  1. 只能指定一个要打包的模块,也就是启动模块
  2. 所有.py文件都不能有中文字符,否则会出现编码异常。
  3. 发布后,可执行文件执行路径不能有中文(最好也不要有空格)。
  4. 启动执行的文件中不要有下面这种判断,否则可执行文件执行会没有任何效果。 
    if __name__ == "__main__": 
    main()


2,用setup.py安装

import sysfrom cx_Freeze import setup, Executable# Dependencies are automatically detected, but it might need fine tuning.build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}# GUI applications require a different base on Windows (the default is for a# console application).base = Noneif sys.platform == "win32":    base = "Win32GUI"setup(  name = "gui",        version = "8.1",        description = "application!",        options = {"build_exe": build_exe_options},        executables = [Executable("fun.py", base=base, targetName = 'launch.exe',icon = "qq.ico")])


出处:http://keliang.blog.51cto.com/3359430/661884


0 0
原创粉丝点击