Nuitka:将源码编译为 `.pyd`
Python 作为一种高级动态语言,以其简洁易用和丰富的生态深受开发者喜爱。然而,Python 代码的解释执行特性往往带来性能上的瓶颈和源码泄露风险。Nuitka 是一个将 Python 源码编译为高效的本地二进制模块(如
.pyd
或可执行文件)的工具,不仅能显著提升运行速度,还能有效保护源码。
环境准备
-
安装 Python(建议版本:3.8 ~ 3.11)
-
安装 Nuitka(建议在虚拟环境中使用):
pip install nuitka
-
Windows 用户需额外安装 C/C++ 编译器,推荐以下任一方式:
- 安装 Visual Studio,并勾选“使用 C++ 的桌面开发”组件
- 或仅安装 Windows SDK(需包含
cl.exe
)
单模块编译
假设你要将 example.py
编译为 example.pyd
,结构如下:
project/
└── example.py
执行以下命令:
python -m nuitka --module example.py --nofollow-imports --no-pyi-file
--module
:将文件编译为.pyd
而不是.exe
--nofollow-imports
:不处理依赖,仅编译该文件--no-pyi-file
:不生成类型提示文件(可选)--remove-output
:删除编译中间文件(可选)--output-dir=build/
:指定所有编译输出文件放入build/
目录(可选)
生成后的 .pyd
文件可直接 import example
使用。
整包编译
假设你有如下包结构:
project/
└── your_package/├── __init__.py├── submodule1.py└── utils/├── __init__.py└── helper.py
使用以下命令:
python -m nuitka --module your_package/ --include-package=your_package --nofollow-imports --no-pyi-file
--module your_package/
:将整个目录作为模块编译--include-package=your_package
:包含包和所有子模块--no-pyi-file
:不生成类型提示文件(可选)--remove-output
:删除编译中间文件(可选)--output-dir=build/
:指定所有编译输出文件放入build/
目录(可选)
自定义构建脚本
为方便批量编译项目中的 Python 源码和拷贝非代码资源,可以编写命令行工具脚本(如示例中的 Nuitcrypt.py
)。以下是使用和注意事项说明:
python -m Nuitcrypt 源目录 [-o 目标目录]
源目录
:必填,待编译的源码根目录(含子目录)-o 目标目录
:可选,编译结果及资源拷贝的输出目录;若不指定,则在原目录内直接替换源码(删除.py
)
import os, shutil, subprocess, sys, importlib.util# 解析命令行参数
if len(sys.argv) < 2:print("[ERROR] 使用方法: python -m Nuitcrypt 源文件夹 [-o 目标文件夹]")sys.exit(1)root = sys.argv[1]
out_dir = sys.argv[3] if len(sys.argv) > 3 and sys.argv[2] == "-o" else None# 检查依赖
print("[INFO] 检查依赖...")
if sys.version_info < (3, 6):print(f"[ERROR] 需要 Python 3.6+,当前: {sys.version}")sys.exit(1)
if not importlib.util.find_spec("nuitka"):print("[ERROR] Nuitka 未安装,运行 'pip install nuitka'")sys.exit(1)
if not os.path.isdir(root):print(f"[ERROR] 源目录 {root} 不存在")sys.exit(1)print("[INFO] 依赖检查通过")# 清理 __pycache__
print("[INFO] 清理 __pycache__...")
for r, dirs, _ in os.walk(root):if "__pycache__" in dirs:path = os.path.join(r, "__pycache__")print(f"[DELETE] 删除缓存: {path}")shutil.rmtree(path, ignore_errors=True)# 编译处理
print("[INFO] 开始编译...")
for r, _, files in os.walk(root):rel = os.path.relpath(r, root)target_dir = r if out_dir is None else os.path.join(out_dir, rel)os.makedirs(target_dir, exist_ok=True)for f in files:src_path = os.path.join(r, f)target_path = os.path.join(target_dir, f)if f.endswith(".py") and f != "__init__.py":cmd = [sys.executable, "-m", "nuitka", "--module", "--no-pyi-file","--nofollow-imports", "--remove-output", f"--output-dir={target_dir}", src_path]print(f"[COMPILING] {src_path}")try:subprocess.run(cmd, check=True, capture_output=True, text=True)print(f"[SUCCESS] 编译成功: {src_path}")if out_dir is None and os.path.exists(src_path):os.remove(src_path)print(f"[REMOVE] 删除源文件: {src_path}")except subprocess.CalledProcessError as e:print(f"[FAIL] 编译失败: {src_path}")print(f"[STDERR] {e.stderr.strip()}")sys.exit(1)elif out_dir is not None:if not os.path.exists(target_path) or not os.path.samefile(src_path, target_path):shutil.copy2(src_path, target_path)print(f"[COPY] {src_path} -> {target_path}")print("[INFO] 编译与拷贝完成")
总结
本文介绍了如何使用 Nuitka 将 Python 源码编译为 .pyd
模块,以提升代码执行效率和保护源代码安全。主要内容包括:
- 环境准备:确保 Python 和 Nuitka 安装到位,Windows 用户需配置 C/C++ 编译器。
- 单模块编译:通过简单命令将单个
.py
文件编译为.pyd
,支持控制是否跟踪依赖、生成类型提示文件等。 - 整包编译:结合
--include-package
参数,将整个 Python 包编译为单一.pyd
,方便统一发布和调用。 - 自定义构建脚本:示范了如何编写命令行工具批量编译项目源码并拷贝非代码资源,提升构建灵活性和自动化水平。