Python-nuitka
1 nuitka的特点
nuitka是一个用来将python代码打包为exe可执行文件,方便其在没有相关环境的windows系统上运行的工具。其原理为:将python代码(自己写的部分)转换成C代码,以提高运行的速度。
相比于pyinstaller,nuitka的的打包速度更慢,但是exe的独立性更高,执行速度更快
2 安装nuitka
安装nuitka,在cmd窗口,输入指令进行网络安装
pip install nuitka
第一次使用nuitka是会要求下载c语言编译器

打包过程中可能会报plugin错误,详见--enable-plugin= 选项

3 Windows环境依赖
核心问题:
在 Windows 系统上,Nuitka 编译出的 --standalone 或 --onefile 可执行文件需要 Microsoft Visual C++ 运行时库 (VCRuntime) 才能运行。与一些其他打包工具(如 PyInstaller)不同,Nuitka 默认不会自动嵌入或打包这些运行时库。如果目标电脑上没有安装对应版本的 VCRuntime,程序就会崩溃并报错FATAL: Nuitka does not work in --standalone or --onefile on Windows without... 。
通用解决方案
在目标计算机上安装 Visual C++ Redistributable
这是最推荐、最彻底的方法。 确保运行您程序的每台 Windows 电脑都安装了正确版本的 Microsoft Visual C++ Redistributable Package。
如何确定版本?
通常,你 编译 Python 程序时使用的 Python 解释器版本 决定了需要的 VCRuntime 版本。
对于 Python 3.5 到最新的 Python 3.11 ,主要依赖 vc_redist.x64.exe (64位) 或 vc_redist.x86.exe (32位)。
特别地,Python 3.5 - 3.9 主要对应 Visual Studio 2015, 2017, 2019的运行时 (包含 vcruntime140.dll)。
Python 3.10+ 可能需要额外包含 vcruntime140_1.dll(2017-2022 的运行时提供了这个)。
下载与安装:
推荐:从微软官方下载 最新、通用的 Visual Studio 2015, 2017, 2019, 2022 Redistributable:
https://aka.ms/vs/17/release/vc_redist.x64.exe (64位)
https://aka.ms/vs/17/release/vc_redist.x86.exe (32位)
这个安装包包含了上述所需的所有运行时,并且会智能地安装目标机器上缺失的部分。
备选:如果你明确知道你的 Python 是用哪个 VS 版本编译的(比如通过 python -c “import sys; print(sys.version)” 查看编译信息行),也可以搜索对应年份的单独安装包。
4 C编译器依赖
首次运行Nuitka时,会跳出以下信息,要求下载C编译器和依赖分析工具到本地电脑

通常下载地址为C:\Users\xxx\AppData\Local\Nuitka\
4 通用选项
--standalone 使得打包结果与本地的Python环境无关,即使得打包结果具备可移植性。
--onefile 使得打包结果为一个可执行文件,而不是一个文件夹。--onefile选项下打出来的包本身就具备可移植性,因此不需要额外加上 --standalone。--onefile 打包结果像绿色软件,仅一个可执行文件;--standalone的打包结果像经过安装的软件,文件夹下包含运行所需要的文件和程序入口。
--lto用于启用链接时间优化。链接时间优化是一种编译器优化技术,它可以在编译和链接阶段对整个程序进行优化,而不仅仅是对单个源文件进行优化。通过启用lto,您可以让编译器在链接时对生成的目标代码进行更深入的优化,提高程序的性能和执行效率
--remove-output 在打包结束后,清理打包过程中生成的临时文件。
--enable-plugin= 启用插件,等号后跟插件名。在要打包的Python代码使用了一些特殊的包时,需要启用插件,Nuitka才能够正确打包。如:如在代码中使用了PySide6,就需要加上--enable-plugin=pyside6。具体的插件列表可以使用nuitka --plugin-list来查看。
anti-bloat Patch stupid imports out of widely used library modules source codes.
data-files Include data files specified by package configuration files.
delvewheel Required for 'support' of delvewheel using packages in standalone mode.
dill-compat Required for 'dill' package and 'cloudpickle' compatibility.
dll-files Include DLLs as per package configuration files.
enum-compat Required for Python2 and 'enum' package.
eventlet Support for including 'eventlet' dependencies and its need for 'dns' package monkey patching.
gevent Required by the 'gevent' package.
gi Support for GI package typelib dependency.
glfw Required for 'OpenGL' (PyOpenGL) and 'glfw' package in standalone mode.
implicit-imports Provide implicit imports of package as per package configuration files.
kivy Required by 'kivy' package.
matplotlib Required for 'matplotlib' module.
multiprocessing Required by Python's 'multiprocessing' module.
no-qt Disable inclusion of all Qt bindings.
options-nanny Inform the user about potential problems as per package configuration files.
pbr-compat Required by the 'pbr' package in standalone mode.
pkg-resources Workarounds for 'pkg_resources'.
playwright Required by 'playwright' package.
pmw-freezer Required by the 'Pmw' package.
pylint-warnings Support PyLint / PyDev linting source markers.
pyqt5 Required by the PyQt5 package.
pyqt6 Required by the PyQt6 package for standalone mode.
pyside2 Required by the PySide2 package.
pyside6 Required by the PySide6 package for standalone mode.
pywebview Required by the 'webview' package (pywebview on PyPI).
spacy Required by 'spacy' package.
tk-inter Required by Python's Tk modules.
transformers Provide implicit imports for transformers package.
upx Compress created binaries with UPX automatically.
--disable-console 在运行打包后的程序时,不会弹出控制台,而是直接运行GUI程序。
--include-package-data= 包含给定软件包名称中的数据文件,等号后软件包名称。有的时候Nuitka并不能正确分析出一些Python软件包所需要使用的数据文件,在运行程序时提示FileNotFoundError等错误,此时就需要使用该选项。如:--include-package-data=ultralytics
--include-data-files= 按文件名包含数据文件,等号后的格式为<SRC=DEST>。SRC指的是文件夹的路径,DEST指的是文件夹相对于打包结果的路径,其中DEST只能使用相对路径。如:--include-data-files=/Users/admin/Downloads/yolov5n.pt=./yolov5n.pt
--include-data-dir= 包含文件夹中的数据文件,等号后的格式为<SRC=DEST>。使用方法与--include-data-files=相同。
5 打包示例
python -m nuitka --standalone --remove-output program_name.py
python -m nuitka --onefile --remove-output program_name.py
python -m nuitka --standalone --disable-console --enable-plugin=pyside6 --remove-output program_name.py
python -m nuitka --macos-create-app-bundle --disable-console --enable-plugin=pyside6 --remove-output program_name.py
6 help信息
Usage: python.exe -m nuitka [--mode=compilation_mode] [--run] [options] main_module.py
Note: For general plugin help (they often have their own
command line options too), consider the output of
'--help-plugins'.
Options:
--help show this help message and exit
--version Show version information and important details for bug
reports, then exit. Defaults to off.
--module Create an importable binary extension module
executable instead of a program. Defaults to off.
--mode=COMPILATION_MODE
Mode in which to compile. Accelerated runs in your
Python installation and depends on it. Standalone
creates a folder with an executable contained to run
it. Onefile creates a single executable to deploy. App
is onefile except on macOS where it's not to be used.
Module makes a module, and package includes also all
sub-modules and sub-packages. Dll is currently under
development and not for users yet. Default is
'accelerated'.
--standalone Enable standalone mode for output. This allows you to
transfer the created binary to other machines without
it using an existing Python installation. This also
means it will become big. It implies these options: "
--follow-imports" and "--python-flag=no_site".
Defaults to off.
--onefile On top of standalone mode, enable onefile mode. This
means not a folder, but a compressed executable is
created and used. Defaults to off.
--python-flag=FLAG Python flags to use. Default is what you are using to
run Nuitka, this enforces a specific mode. These are
options that also exist to standard Python executable.
Currently supported: "-S" (alias "no_site"),
"static_hashes" (do not use hash randomization),
"no_warnings" (do not give Python run time warnings),
"-O" (alias "no_asserts"), "no_docstrings" (do not use
doc strings), "-u" (alias "unbuffered"), "isolated"
(do not load outside code), "-P" (alias "safe_path",
do not used current directory in module search) and
"-m" (package mode, compile as "package.__main__").
Default empty.
--python-debug Use debug version or not. Default uses what you are
using to run Nuitka, most likely a non-debug version.
Only for debugging and testing purposes.
--python-for-scons=PATH
When compiling with Python 3.4 provide the path of a
Python binary to use for Scons. Otherwise Nuitka can
use what you run Nuitka with, or find Python
installation, e.g. from Windows registry. On Windows,
a Python 3.5 or higher is needed. On non-Windows, a
Python 2.6 or 2.7 will do as well.
--main=PATH If specified once, this takes the place of the
positional argument, i.e. the filename to compile.
When given multiple times, it enables "multidist" (see
User Manual) it allows you to create binaries that
depending on file name or invocation name.
Control the inclusion of modules and packages in result:
--include-package=PACKAGE
Include a whole package. Give as a Python namespace,
e.g. "some_package.sub_package" and Nuitka will then
find it and include it and all the modules found below
that disk location in the binary or extension module
it creates, and make it available for import by the
code. To avoid unwanted sub packages, e.
Python Nuitka打包工具介绍

最低0.47元/天 解锁文章
6080

被折叠的 条评论
为什么被折叠?



