内容只提供Windows 10 与 Windows 11 下,搭建 Python 3.12 环境,并使用 Nuitka 将脚本打包为可执行文件的详细流程。全文分为以下几部分:
-
准备工作与系统要求
-
安装 Python 3.12
-
配置环境变量与 pip
-
创建虚拟环境(推荐)
-
安装编译工具链
-
安装 Nuitka 及相关依赖
-
编写示例脚本
-
使用 Nuitka 打包
-
常见问题与排错
-
附录:常用命令汇总
1. 准备工作与系统要求
-
操作系统:Windows 10(1909+以上)或 Windows 11
-
权限:管理员权限(安装编译器、修改系统环境变量时需要)
-
网络:能够访问 python.org 和 PyPI 源
2. 安装 Python 3.12
-
下载安装包
-
访问官网:Python Releases for Windows | Python.org
-
选择 Windows installer (64-bit) for Python 3.12.x MSI 安装包
-
-
运行 MSI 安装程序
-
勾选 “Add Python 3.12 to PATH”
-
选择 “Customize installation” → 勾选 pip、tcl/tk、文档等 → “Next”
-
勾选 “Install for all users”(若有管理员权限)
-
安装完成后,弹出提示 “Setup was successful”
-
-
验证安装
python --version # 应输出:Python 3.12.x pip --version
3. 配置环境变量与 pip
-
PATH 自动配置:如安装时已勾选,PATH 中应包含
C:\Program Files\Python312\
及...Scripts\
-
更新 pip、setuptools、wheel
python -m pip install --upgrade pip setuptools wheel
4. 创建虚拟环境(推荐)
在项目根目录下执行:
python -m venv venv
激活虚拟环境:
-
PowerShell:
.\venv\Scripts\Activate.ps1
-
CMD:
.\venv\Scripts\activate.bat
激活后,命令行提示符前会出现 (venv)
。
5. 安装编译工具链
Nuitka 在 Windows 上默认使用 MSVC(Visual Studio Build Tools),也可选用 MinGW。
5.1 MSVC(推荐)
-
下载并安装 Visual Studio Build Tools 2022
-
勾选 “C++ build tools” 工作负载,确保包含 “Windows 10 SDK”
安装完成后,打开 “x64 Native Tools Command Prompt for VS 2022” 或在 PowerShell/CMD 预先运行:
call "C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
5.2 MinGW(可选)
-
安装 mingw-w64
-
将 MinGW 的
bin
目录加入 PATH,例如C:\mingw-w64\mingw64\bin
6. 安装 Nuitka 及相关依赖
确保虚拟环境已激活,然后:
pip install nuitka
# 若需要 GUI 支持,需安装 pillow、tkinter 等
# pip install pillow
检查安装:
python -c "import nuitka; print(nuitka.__version__)"
7. 编写示例脚本
在项目目录中新建 hello.py
,内容如下:
def main():print("Hello, Nuitka 打包!")if __name__ == "__main__":main()
8. 使用 Nuitka 打包
下面以几种常用方式示例:
8.1 简单编译
nuitka --standalone --mingw64 hello.py
-
--standalone
:生成独立目录,可跨机器运行 -
--mingw64
:指定使用 MinGW 编译(若使用 MSVC 可省略此参数)
完成后,会在当前目录生成 hello.dist\
,其中包含可执行文件 hello.exe
及所需 DLL。
8.2 生成单文件
nuitka --onefile --standalone hello.py
-
--onefile
:将所有文件打包成一个 exe -
注意:单文件模式下首次启动会有解包延迟
8.3 禁用控制台窗口(GUI 程序)
nuitka --onefile --windows-disable-console gui_app.py
8.4 进阶选项
-
--enable-plugin=tk-inter
:支持 Tkinter -
--enable-plugin=brotli
:支持 Brotli 压缩 -
--lto=yes
:启用链接时优化 -
--nofollow-import-to=<module>
:排除某些模块
可通过 nuitka --help
查看完整参数列表。
9. 常见问题与排错
问题 | 解决办法 |
---|---|
“Visual C++ build tools not found” | 需安装 Visual Studio Build Tools,并运行 vcvars64.bat。 |
打包后运行缺少 | 确保使用 |
单文件模式运行卡顿 | 可尝试移除 |
打包 GUI 应用无界面 | 使用 |
10. 附录:常用命令汇总
# 更新工具
python -m pip install --upgrade pip setuptools wheel# 创建并激活 venv
python -m venv venv
.\venv\Scripts\activate.bat # CMD
.\venv\Scripts\Activate.ps1 # PowerShell# 安装 Nuitka
pip install nuitka# MSVC 环境配置(示例路径)
call "C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"# 基础打包
nuitka --standalone hello.py# 单文件
nuitka --onefile --standalone hello.py# 禁用控制台
nuitka --onefile --windows-disable-console gui_app.py# 启用 LTO
nuitka --standalone --lto=full app.py
最后,您已完成在 Windows 10/11 上搭建 Python 3.12 环境,并了解使用 Nuitka 进行打包的相关流程。根据项目需求,可在此基础上添加更多插件及自定义编译选项。