使用nuitka打包python为exe并发布
简介
Nuitka is the optimizing Python compiler written in Python that creates executables that run without an need for a separate installer. Data files can both be included or put alongside.
Nuitka is fully compatible with Python 3 (3.4 — 3.11) and Python 2 (2.6, 2.7), works on Windows, macOS, Linux and more, basically where Python works for you already.
使用此库可达到以下目的:
隐藏源码。这里的pyinstaller是通过设置key来对源码进行加密的;而nuitka则是将python源码转成C++(这里得到的是二进制的pyd文件,防止了反编译),然后再编译成可执行文件。
方便移植:跨平台,可多平台使用:win、mac、linux等
优势
缺点
安装
1 2 3 pip install Nuitka pip install ordered-set pip install zstandard
1 2 pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Nuitka
打包发布
1 2 3 4 5 6 7 python -m nuitka --mingw64 --standalone --follow-import-to=utils,api,log ,run --output-dir=out --show-progress ./run/run_py.py
常见command option 解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 –mingw64 –standalone –windows-disable-console –output-dir=out –show-progress –show-memory –enable-plugin=pyside6 –plugin-enable=tk-inter –plugin-enable=numpy –plugin-enable=torch –plugin-enable=tensorflow –windows-icon-from-ico=你的.ico –windows-company-name=Windows下软件公司信息 –windows-product-name=Windows下软件名称 –windows-file-version=Windows下软件的信息 –windows-product-version=Windows下软件的产品信息 –windows-file-description=Windows下软件的作用描述 –windows-uac-admin=Windows下用户可以使用管理员权限来安装 –linux-onefile-icon=Linux下的图标位置 –onefile –include-package=复制比如numpy,PyQt5 –include-module=复制比如when.py –-include-package-data=包含给定软件包名称中的数据文件,等号后软件包名称。 –include-package-data=ultralytics –-include-data-files= 按文件名包含数据文件, -–include-data-dir= 包含文件夹中的数据文件, –follow-import-to=MODULE/PACKAGE --enable-plugin --plugin-list --remove-output --windows-icon-from-ico --msvc --company-name --product-name --file-version --product-version --file-description
mingw64下载缓慢
1 https://www.mingw-w64.org/downloads/
文件名如下
1 winlibs-x86_64-posix-seh-gcc-13.2.0-llvm-16.0.6-mingw-w64msvcrt-11.0.1-r1.zip
需要手动移动至以下目录(无法自定义目录,只能使用默认路径)
1 C:\Users\Administrator\AppData\Local\Nuitka\Nuitka\Cache\downloads
放置完成后,重新运行上述命令即可打包成功!
当控制台询问是否安装gcc时, 输入yes进行安装
打包完成后,效果如下:
如题
pyinstaller 与 nukia打包结果对比
大小对比
如图
启动速度对比
nuitkia比pyinstaller打包exe启动同一程序快66%
特殊库引入
1 2 3 4 5 6 --plugin-enable=pyqt5 --windows-disable-console --windows-icon-from-ico=favicon.ico
1 nuitka --mingw64 --standalone --show-progress --show-memory --output-dir=out --plugin-enable=pyqt5 --windows-disable-console --windows-icon-from-ico=favicon.ico xxx.py
See