Python打包可执行文件-cx_Freeze

目录

简介

安装

问题处理

使用

打包


简介

之前有篇文章有使用pyinstaller打包.exe文件,但是没有详细介绍过,这次整理下

python入门-从安装环境配置(Anaconda)到做一个下班打卡提醒.exe小工具_瑶山的博客-CSDN博客

Python打包可执行文件-cx_Freeze_第1张图片

Python程序发布时,通常包括以下三种形式

  • py源码:即源程序代码,使用时需要Python执行环境(包含对应的依赖库)
  • pyc形式:由py文件编译生成,源码私密性提升、运行速度提升
  • 可执行文件:可直接运行的程序文件,不同平台(Mac/Linux/Windows…)下的格式不同,与平台强关联。无须额外下载依赖程序

Python打包可执行程序几种工具,Freezing Your Code — The Hitchhiker's Guide to Python

Solution Windows Linux OS X Python 3 License One-file mode Zipfile import Eggs pkg_resources support Latest release date
bbFreeze yes yes yes no MIT no yes yes yes Jan 20, 2014
py2exe yes no no yes MIT yes yes no no Oct 21, 2014
pyInstaller yes yes yes yes GPL yes no yes no Jul 9, 2019
cx_Freeze yes yes yes yes PSF no yes yes no Aug 29, 2019
py2app no no yes yes MIT no yes yes yes Mar 25, 2019

pyInstaller与cx_Freeze的通用性最强(全平台支持、支持Python3),pyInstaller之前有写过这次就实验下cx_Freeze

安装

环境:Python 3.7.6

# 安装
pip install cx_Freeze

等待下载

Python打包可执行文件-cx_Freeze_第2张图片

问题处理

有个提示处理下

由于依赖关系冲突。

spyder 4.0.1要求pyqt5<5.13;python_version>=“3”,未安装。

spyder 4.0.1要求pyqtwebengine<5.13;python_version>=“3”,未安装。

查看spyder版本

pip install spyder==*

pip install spyder==5.4.4

或者pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ spyder==5.4.4,这个比较快

python-for-android 2020.6.2 has a non-standard dependency specifier pep517<0.7.0"

pip install "pep517<0.7.0"

pip show pep517

其他错误都可以根据 根据错误提示切换或下载对应安装包版本

使用

验证 cxfreeze -h

  File "D:\Anaconda\lib\site-packages\cx_Freeze\parser.py", line 29, in
    lief.logging.set_level(lief.logging.LOGGING_LEVEL.ERROR)
AttributeError: module 'lief' has no attribute 'logging'

重装了lief和更换版本都不行,我找到提示所在文件parser.py打开如下

Python打包可执行文件-cx_Freeze_第3张图片

找到报错位置,注释掉,再次执行cxfreeze -h

Python打包可执行文件-cx_Freeze_第4张图片

打包

脚本setup.py

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(
    # your Application Name
    name="love",
    version="1.0",
    # Your Application Description
    description="application description",
    author="ys",
    executables=[Executable("your xx.py", base=base)]
)

python setup.py build,执行完后有一个build文件夹,build->exe.win-amd64-3.7下有.exe可执行文件,

Python打包可执行文件-cx_Freeze_第5张图片

 打开如下,与编辑器运行效果相同

Python打包可执行文件-cx_Freeze_第6张图片

你可能感兴趣的:(python,开发语言,cx_Freeze,打包)