python windows打包成可执行文件的几种方式
发布时间:2022-07-24 06:10:17 阅读:470

Python打包工具几种方式

py2exe,pyinstaller,cx_Freeze,nuitka

py2exe

py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序。

  • 优点:

    可以把python打包成exe

  • 缺点:

    打包好的 exe只能在相同的系统下运行,比如你在XP系统用py2exe打包好的exe只能在XP上运行,在win7上肯定有问题,而且也是存在在别的XP机子上也可能不能运行,因为一些别的机子可能会缺少dll文件什么的;
    打包的文件也需要打包同exe下的dll,和一些库的压缩包,不然exe根本跑不起来
    打包时候报始终报RuntimeError: BeginUpdateResource错误,就需要拷贝dll文件。C:\WINDOWS\system32\python27.dll复制到dist目录

总之毛病一大堆,基本上已经舍弃了

pyinstaller

PyInstaller除了win32平台外还支持Linux,Unix平台.py2exe的用法在前面的里已经有了,现在看看PyInstaller,pyinstaller是目前应用最多的python打包工具。

PyInstaller本身并不是Python模块,所以安装时随便放在哪儿都行。

  • 优点:

    可将python文件转成可执行文件
    跨平台
    输出的可以是单一目录,也可以是一个单独的打好包的可执行文件。py2exe貌似输出目录。
    智能支持python的第三方模块如PyQt,外部数据文件等
    支持EGG格式文件
    可执行文件可以用UPX压缩,二进制压缩方式
    支持控制台和视窗两种方式。
    可以选择exe文件的图标 (Windows only)
    支持 COM server (Windows only)

  • 缺点:

    import导入的问题
    pyinstaller是很智能的,只要指定了入口py文件,那么它就会根据代码自动查找需要导入的包。但是隐式导入的话,平常运行是没有问题的:

# test1.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
 
 
DB_CONNECT_STRING = 'mysql+pymysql://root:123456@localhost/study'
engine = create_engine(DB_CONNECT_STRING, echo = False)
DB_Session = sessionmaker(bind = engine)
session = DB_Session()
 
print('this is my test')

运行这个ORM库的初始化引擎,是没有问题的,在console得到结果:
this is my test

那么我们开始打包,使用最简单的pyinstaller test1.py。打包完成后,在当前目录下有个dist文件夹,进入dist下的test1文件夹,然后打开cmd,运行这个exe,我们就会发现:

提示: no model named ‘pymysql’

这是怎么回事呢?那么问题来了,sqlalchemy这个库在初始化的时候是不需要显示导入引擎库的,它自己有一个create_engine()的函数来初始化,这个字符串是使用者根据规则来自己填写的。其实解决的方法很简单,我们只要在显式导入pymysql这个库即可。现在我们导入这个库:

import pymysql

重新打包一遍(重新打包的时候记得删除掉spec文件,否则会有缓存,或者是加上–clean选项清除掉),再次运行,现在就没有这个问题了。

生成产物可反编译

cx_Freeze

python中比较常用的python转exe方法有三种,分别是cx_freeze,py2exe,PyInstaller。py2exe恐怕是三者里面知名度最高的一个,但是同时相对来说它的打包质量恐怕也是最差的一个。pyinstaller打包很好,但是操作工序较为复杂,推荐cx_freeze,可以通过pip install cx-freeze 安装。这是别人的描述

生成产物可反编译

nuitka

Nuitka直接将python编译成C++代码 ,再编译C++代码产生可执行文件,完全不存在反向解析的问题,非常安全,而且由于可执行文件由C++编译而来,运行速度也会获得提升。

下载完后会提示安装编译c++代码的软件,使用的是gcc

安装好有会有一个MinGW64的文件夹,比如我要打包一个back.py的文件,把这个py文件放在哪个目录下,常用命令如下

python -m nuitka --recurse-all --standalone cc.py

python -m nuitka --standalone back.py

可以用的打包方式

python -m nuitka --recurse-all --recurse-plugins=D:\Python\Lib --remove-output --plugin-enable=enum_compat back.py

带图标并且有窗口

python -m nuitka --recurse-all --recurse-plugins=D:\Python\Lib --remove-output --icon=logo.ico --plugin-enable=enum_compat back.py

不带窗口不带图标

python -m nuitka --recurse-all --recurse-plugins=D:\Python\Lib --remove-output --plugin-enable=enum_compat --windows-disable-console back.py

不删除辅助文件夹

python -m nuitka --recurse-all --recurse-plugins=D:\Python27\Lib --icon=logo.ico --plugin-enable=enum_compat back.py

打包命令

python -m nuitka --standalone --mingw64 --show-memory --show-progress --follow-imports --plugin-enable=pylint-warnings --plugin-enable=implicit-imports --include-module=generator --include-data-dir=generator/generator/lib_binding=generator/generator/lib_binding --include-data-dir=generator/parser/schema=generator/parser/schema --include-data-dir=generator/templates=generator/templates --include-data-dir=ddsidltoCode=ddsidltoCode  --output-dir=nuitka .\aragen

测试命令

.\aragen.exe .\Production\Application\crypto\Instance\ServiceInstance_crypto.arxml .\Production\Application\radar_service\Deploy\ServiceDeploy_radar_service.arxml .\Production\Application\radar_service\Design\ServiceDesign_radar_service.arxml .\Production\Application\radar_service\Execution\ExecutionDescription_radar_service.arxml .\Production\Application\radar_service\Instance\ServiceInstance_radar_service.arxml .\Production\Application\radar_service\Machine\ethernet_sdg_define.arxml .\Production\Application\radar_service\Machine\MachineDescription_radar_service.arxml .\Production\Application\radar_service\System\DataType_radar_service.arxml .\Production\Execution\radar_service\ExecutionDescription_radar_service.arxml .\Production\Execution\ucm\ExecutionDescription_ucm.arxml .\Production\Machine\machine\MachineDescription_machine.arxml .\Production\System\network.arxml .\Production\System\network_manifest.arxml .\Production\System\sample_system\MachineDesign_sample_system.arxml .\Production\System\service_discovery.arxml .\Production\System\stdcpptypes.arxml -v -o out_code
发表评论
使用 Nuxt 3 构建 | 部署于 Kubernetes | 托管于 狗云
Copyright © 2020-2024 | 网站已续航 1530 天