### 使用Nuitka打包成exe ![](https://img.kancloud.cn/b0/6e/b06e0c4df8f1a74a297e1799c794112a_927x551.png) 官网:https://www.nuitka.net/ #### 1.py安装 Nuitka 模块 **cmd输入pip install Nuitka 指令** 或者 **pip install nuitka -i https://pypi.tuna.tsinghua.edu.cn/simple** **pip install nuitka --use-pep517 -i https://pypi.tuna.tsinghua.edu.cn/simple** ![](https://img.kancloud.cn/73/de/73de966b83451d04b9bb170a049c9215_1230x99.png) 已经安装会这样显示 zstandard这个模块会让文件更小 可以安装 **pip install zstandard -i https://pypi.tuna.tsinghua.edu.cn/simple** #### 2.安装 MinGW64 这个是把py程序编译成c++语言的好像,要这个才能打包 官网:https://sourceforge.net/projects/mingw-w64/files/ 网盘: https://share.weiyun.com/TVJaVneZ 我是下载这个,这个不是安装包,直接是源码需要配置path ![](https://img.kancloud.cn/05/8e/058eff4baae9a6d846e835c4bdf0cc73_924x702.png) 下载解压后放到E盘,路径就是E:\mingw64,这步你随意 ![](https://img.kancloud.cn/c5/9c/c59c11aae411629fe0a8d113afa409b2_822x469.png) 然后配置环境变量,我的电脑,属性,系统,高级系统设置 ![](https://img.kancloud.cn/a9/02/a902d2df058b2568ad811c5ac303e2df_1641x750.png) 然后 新打开cmd窗口输入 **gcc -v** 指令查看MinGW64 安装成功没 ![](https://img.kancloud.cn/7c/b3/7cb38a3d47bdf9c1522bbcf7cf2c69d0_1214x502.png) 到这步就可以使用Nuitka编译exe了 3.编译py脚本成exe文件 在有py脚本的文件夹打开cmd窗口。win11 可以鼠标右键打开,其他的按shift+鼠标右键 ![](https://img.kancloud.cn/36/f5/36f53775f8bfd4099e0ba84fe297e5e7_1233x552.png) 然后输入指令按回车 ``` // 打包成文件夹形式 nuitka --standalone --mingw64 main.py // 打包单个exe形式 nuitka --standalone --mingw64 --onefile main.py --remove-output参数:删除临时缓存 --mingw64 #默认为已经安装的vs2017去编译,否则就按指定的比如mingw(官方建议) ``` 这样编译放到其他电脑不需要安装环境,会生成这两文件夹,exe文件在***dist文件夹里面。 ![](https://img.kancloud.cn/50/0b/500b1f902ef18bcf707fdd76e816404c_273x88.png) . .**其他的编译方法还是需要学习,目前就这些简单的** . 官网:https://nuitka.net/doc/user-manual.html 其他指令: nuitka --mingw64 ---standalone --output-dir=ayck 你的.py 指令: ``` --version 显示程序的版本号并退出 -h, --help 显示此帮助信息并退出 --module 创建一个扩展模块可执行文件,而不是 程序。默认为关闭。 --standalone 为输出启用独立模式。这使您可以 将创建的二进制文件传输到其他机器,而无需 它使用现有的 Python 安装。这也是 意味着它会变大。它暗示了这些选项:“-- follow-imports”和“--python-flag=no_site”。默认值 关闭。 --onefile 在独立模式之上,启用 onefile 模式。这 表示不是文件夹,而是压缩的可执行文件 创建和使用。默认为关闭。 --python-debug 是否使用调试版本。默认使用你是什么 用于运行 Nuitka,很可能是非调试版本。 --python-flag=FLAG 要使用的 Python 标志。默认是你用来 运行 Nuitka,这会强制执行特定模式。这些是 标准 Python 可执行文件也存在的选项。 当前支持:“-S”(别名“no_site”), “static_hashes”(不使用散列随机化), “no_warnings”(不给出 Python 运行时警告), “-O”(别名“no_asserts”)、“no_docstrings”(不要使用 文档字符串)、“-u”(别名“无缓冲”)和“-m”。 默认为空。 --python-for-scons=PATH 如果使用 Python3.3 或 Python3.4,请提供一个路径 用于 Scons 的 Python 二进制文件。否则 Nuitka 可以 使用运行 Nuitka 的程序或“scons”二进制文件 在 PATH 或 Python 安装中找到 Windows 注册表。 --warn-implicit-exceptions 启用对检测到的隐式异常的警告 编译时间。 --warn-unusual-code 为编译时检测到的异常代码启用警告 时间。 --assume-yes-for-downloads 如有必要,允许 Nuitka 下载外部代码, 例如依赖 walker,ccache,甚至 gcc 视窗。要禁用,从 nul 设备重定向输入, 例如“</dev/null”或“<NUL:”。默认为提示。 控制结果中包含的模块和包: --include-package=包 包括一个完整的包裹。作为 Python 命名空间, 例如“some_package.sub_package” 然后 Nuitka 将 找到它并包含它以及下面找到的所有模块 二进制或扩展模块中的磁盘位置 它创建并使其可供 代码。为了避免不需要的子包,例如考验你 例如可以执行此“--nofollow-import-to=*.tests”。 默认为空。 --include-module=模块 包括一个模块。作为 Python 命名空间, 例如“some_package.some_module” 然后 Nuitka 将 找到它并将其包含在二进制文件或扩展中 它创建的模块,并使其可用于导入 代码。默认为空。 --include-plugin-directory=模块/包 包括该目录的内容,无论是否 给定的主程序以可见的形式使用它。 覆盖所有其他包含选项。可以给 多次。默认为空。 --include-plugin-files=模式 包含到与 PATTERN 匹配的文件中。覆盖所有 其他跟随选项。可以多次给予。 默认为空。 --prefer-源代码 对于已经编译好的扩展模块,有 源文件和扩展模块,通常 使用了扩展模块,但应该会更好 从可用的源代码编译模块 最棒的表演。如果不需要,有--no- prefer-source-code 禁用有关它的警告。 默认关闭。 将以下内容控制到导入的模块中: --follow-stdlib 也从标准下降到导入的模块 图书馆。这将增加编译时间 很多。默认为 ``` ### 可能出现问题 **问题0** 安装 ordered-set 模块 ![](https://img.kancloud.cn/a1/e6/a1e6cc3812bb558c1a47584373d1b9f9_1655x139.png) ``` pip install ordered-set ``` **问题1** 这个可以用上面下载的MinGW64替换,问题不大。但是这个是新版,建议替换上面那个 ``` Is it OK to download and put it in 'C:\Users\Administrator\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\11.3.0-14.0.3-10.0.0-msvcrt-r3'. No installer needed, cached, one time question. Proceed and download? [Yes]/No ``` ![](https://img.kancloud.cn/dd/ab/ddab0579d74c2e65bbd3b1d1d5099e3d_1728x455.png) 下载文件:https://github.com/brechtsanders/winlibs_mingw/releases/download/11.3.0-14.0.3-10.0.0-msvcrt-r3/winlibs-x86_64-posix-seh-gcc-11.3.0-llvm-14.0.3-mingw-w64msvcrt-10.0.0-r3.zip 网盘下载:https://share.weiyun.com/kVIjdS00 下载解压到C:\Users\Administrator\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\11.3.0-14.0.3-10.0.0-msvcrt-r3文件夹 ![](https://img.kancloud.cn/95/de/95defc75010169735e49aa9c4f0b6826_915x340.png) . **问题2** ``` Is it OK to download and put it in 'C:\Users\Administrator\AppData\Local\Nuitka\Nuitka\Cache\downloads\ccache\v4.6'. No installer needed, cached, one time question. Proceed and download? [Yes]/No ``` ![](https://img.kancloud.cn/9e/2b/9e2b8690879935a2f649d4860a4834a6_1648x345.png) 下载文件:https://github.com/ccache/ccache/releases/download/v4.6/ccache-4.6-windows-32.zip 网盘下载:https://share.weiyun.com/abQimaGy 下载解压到 C:\Users\Administrator\AppData\Local\Nuitka\Nuitka\Cache\downloads\ccache\v4.6 ![](https://img.kancloud.cn/ac/6f/ac6f77f0267cb0d250d32bae257812fe_1108x351.png) . **问题3** ``` to analyze the dependencies of Python extension modules. Is it OK to download and put it in 'C:\Users\ws\AppData\Local\Nuitka\Nuitka\Cache\downloads\depends\x86_64'. No installer needed, cached, one time question. Proceed and download? [Yes]/No ``` ![](https://img.kancloud.cn/6b/5e/6b5e35ea8d4f1206a44915557783a0ea_1374x270.png) 下载链接:https://dependencywalker.com/depends22_x64.zip 网盘链接:https://share.weiyun.com/uRJzES29 下载解压到 C:\Users\Administrator\AppData\Local\Nuitka\Nuitka\Cache\downloads\depends\x86_64 ![](https://img.kancloud.cn/03/a0/03a071341111750e4d807d1415971822_1315x296.png)