当前位置: 首页 > java >正文

Nuitka:将源码编译为 `.pyd`

Python 作为一种高级动态语言,以其简洁易用和丰富的生态深受开发者喜爱。然而,Python 代码的解释执行特性往往带来性能上的瓶颈和源码泄露风险。Nuitka 是一个将 Python 源码编译为高效的本地二进制模块(如 .pyd 或可执行文件)的工具,不仅能显著提升运行速度,还能有效保护源码。

环境准备

  1. 安装 Python(建议版本:3.8 ~ 3.11)

  2. 安装 Nuitka(建议在虚拟环境中使用):

    pip install nuitka
    
  3. 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,方便统一发布和调用。
  • 自定义构建脚本:示范了如何编写命令行工具批量编译项目源码并拷贝非代码资源,提升构建灵活性和自动化水平。
http://www.xdnf.cn/news/16811.html

相关文章:

  • 对于前端工程化的理解
  • Product Hunt 每日热榜 | 2025-07-31
  • PyQt GUI开发初学者:固定尺寸还是全屏自适应?
  • Table-Render:基于 JSON Schema 的高性能 React 动态表格渲染器
  • ros2--参数指令--rqt
  • 动手学习深度学习-深度学习知识大纲
  • VuePress 使用详解
  • 转码刷 LeetCode 笔记[1]:3.无重复字符的最长子串(python)
  • (1-7-6)Mysql 常用的基本函数
  • JVM问题分析处理手册
  • LeetCode 面试经典 150_数组/字符串_买卖股票的最佳时机(7_121_C++_简单)(贪心)
  • 【javascript】new.target 学习笔记
  • 【2025/07/31】GitHub 今日热门项目
  • DAY16-结构体
  • linux如何将两份hdmi edid合并
  • system.conf linux用于启动和管理系统进程的初始化系统和服务管理器的配置文件
  • WEditor:高效的移动端UI自动化脚本可视化编辑器
  • 【云故事探索】NO.16:阿里云弹性计算加速精准学 AI 教育普惠落地
  • 力扣 Pandas 挑战(6)---数据合并
  • 基于SpringBoot和SpringAI框架实践
  • Google政策大更新:影响金融,Ai应用,社交,新闻等所有类别App
  • 【科研绘图系列】R语言绘制线性相关性
  • 算法训练营day37 动态规划⑤ 完全背包 518. 零钱兑换 II、 377. 组合总和 Ⅳ、70. 爬楼梯 (进阶)
  • 排序的演进:从机械齿轮到领域专用芯片加速器的全面综述
  • 最新PS 2025安装包下载与安装教程(Adobe Photoshop 2025 )
  • 【数据结构初阶】--二叉树(六)
  • 乱删文件,电脑不能开机,怎么办
  • 【C语言】深度剖析指针(三):回调机制、通用排序与数组指针逻辑
  • DeepSeek笔记(三):结合Flask实现以WEB方式访问本地部署的DeepSeek-R1模型
  • opencv解迷宫