python使用cx_Freeze打包为exe&msi

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/setlilei/article/details/100547093

python中常用打包exe方法有三种 cx_freeze py2exe pyinstaller py2exe知名度较高 相对来说打包质量较差 pyinstaller打包很好 但操作较复杂 本文使用cx_freeze

安装依赖

pip install cx-freeze

# 如上命令执行完后script目录增加三个文件
cxfreeze
cxfreeze-postinstall
cxfreeze-quickstart

# 在scripts执行如下命令
python cxfreeze-postinstall
# 如上命令执行完晚后 scripts文件夹下会增加cxfreeze.bat cxfreeze-quickstart.bat
cxfeeeze -h # 执行此行命令出现帮助文档即安装成功

使用命令打包为exe

cxfreeze D:\test\test.py --install-dir=D:\test
# 如上命令执行完后会出现如下文件
lib # 依赖包
test.exe # 执行文件
python37.dll # python动态链接库

# 如果程序为图片化界面可再加上'--base-name=Win32GUI'参数 可在运行exe时去掉cmd黑框
cxfreeze D:\test\test.py --install-dir=D:\test --base-name=Win32GUI

使用setup.py打包为exe

setup.py文件内容如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '李磊'

from cx_Freeze import setup, Executable

setup(name='test',
      version='1.0',
      description='test',
      executables=[Executable("test.py")]
      )

执行python setup.py build 会在项目build\exe.win-amd64-3.7目录下生成exe文件


在setup.py的基础上打包成msi(可安装文件)
python setup.py bdist_msi 会在项目dist目录下生成msi文件


完整setup.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '李磊'

import sys

from cx_Freeze import setup, Executable

base = None
# 判断windows系统
if sys.platform == 'win32':
    base = 'Win32GUI'

packages = []
# 动态引入
for dbmodule in ['win32gui', 'win32api', 'win32con', 'cx_Freeze']:
    try:
        __import__(dbmodule)
    except ImportError:
        pass
    else:
        packages.append(dbmodule)

options = {
    'build_exe':
        {
            'includes': 'atexit'
            , "packages": packages  # 依赖的包
            , 'include_files': ['image']  # 额外添加的文件 可以是文件夹
        }
}

executables = [
    Executable(
        'test.py'  # 入口文件
        , base=base
        , targetName='test.exe'  # 生成的exe的名称
        , icon="favicon.ico"  # 生成的exe的图标
    )
]

setup(
    name='test',
    version='1.0',
    description='描述',
    options=options,
    executables=executables
)

持续更新。。。

猜你喜欢

转载自blog.csdn.net/setlilei/article/details/100547093
今日推荐