I Built a Python Compiler That Generates Native Executables with Rust

Python is an incredibly productive language. But when it comes to performance, native interoperability and application distribution, there are still interesting problems to solve. What if we could keep writing Python while compiling it into native executables? That’s the idea behind Pyrock, an experimental open-source Python compiler I’ve been developing at Abstrkshen.

Python 是一门生产力极高的语言。但在性能、原生互操作性和应用程序分发方面,仍有一些有趣的问题需要解决。如果我们能在保持编写 Python 代码的同时将其编译为原生可执行文件会怎样?这就是我在 Abstrkshen 开发的实验性开源 Python 编译器 Pyrock 背后的理念。

The experiment Pyrock takes supported Python source code, performs type inference, generates Rust code and compiles it into a native executable. The goal is to preserve familiar Python syntax while taking advantage of native compilation wherever possible.

Pyrock 实验项目接收受支持的 Python 源代码,执行类型推断,生成 Rust 代码并将其编译为原生可执行文件。其目标是在尽可能利用原生编译的同时,保留熟悉的 Python 语法。

Python source ↓ AST parsing ↓ Type inference ↓ Rust code generation ↓ Native compilation ↓ Executable

Python 源码 ↓ AST 解析 ↓ 类型推断 ↓ Rust 代码生成 ↓ 原生编译 ↓ 可执行文件

Performance In one of my numeric-loop benchmarks, Pyrock achieved approximately 50× faster execution than CPython. In another demonstration, the same Python program produced identical output while running significantly faster. The resulting native executable was approximately 458 KB and did not require a Python interpreter to run. These results are workload-specific. They do not mean that every Python program will automatically become 50× faster.

性能:在我的一项数值循环基准测试中,Pyrock 的执行速度比 CPython 快了约 50 倍。在另一个演示中,同一个 Python 程序在运行速度显著提升的同时,产生了完全相同的输出。生成原生可执行文件的大小约为 458 KB,且运行无需 Python 解释器。这些结果因工作负载而异,并不意味着每个 Python 程序都会自动提速 50 倍。

Importing Rust and C directly One of the features I’m particularly interested in is native language interoperability. Instead of manually creating bindings, Pyrock allows supported Python programs to import Rust and C source files directly. The compiler handles the integration during the build process. The idea is to make Python a convenient entry point for software that also needs native performance or access to existing native libraries.

直接导入 Rust 和 C:我特别感兴趣的功能之一是原生语言互操作性。Pyrock 允许受支持的 Python 程序直接导入 Rust 和 C 源代码文件,而无需手动创建绑定。编译器会在构建过程中处理集成。其理念是让 Python 成为软件的便捷入口,同时满足对原生性能或访问现有原生库的需求。

Why build another Python compiler? Projects such as Cython, Nuitka, Numba and mypyc already address different aspects of Python performance and compilation. Pyrock is an experiment exploring a particular combination of goals: Familiar Python syntax. Automatic type inference. Native executable generation. Direct Rust and C interoperability. Minimal runtime dependencies for supported programs. Transparent reporting of what can and cannot be compiled natively. I’m interested in how much of Python can be compiled without sacrificing the developer experience that makes the language attractive in the first place.

为什么要构建另一个 Python 编译器?Cython、Nuitka、Numba 和 mypyc 等项目已经解决了 Python 性能和编译的不同方面。Pyrock 是一个旨在探索特定目标组合的实验:熟悉的 Python 语法、自动类型推断、原生可执行文件生成、直接的 Rust 和 C 互操作性、受支持程序的最少运行时依赖,以及对哪些内容可以或不可以原生编译的透明报告。我感兴趣的是,在不牺牲使该语言最初具有吸引力的开发体验的前提下,Python 有多少部分可以被编译。

The difficult part: Python semantics Generating Rust code is only part of the problem. Python’s dynamic behavior introduces significant challenges: Dynamic typing. Runtime attribute resolution. Arbitrary-precision integers. Dynamic function dispatch. Exception handling. Third-party package compatibility. Object identity and mutation.

难点:Python 语义。生成 Rust 代码只是问题的一部分。Python 的动态行为带来了重大挑战:动态类型、运行时属性解析、任意精度整数、动态函数分发、异常处理、第三方包兼容性以及对象标识和可变性。

Pyrock is still experimental. Some operations compile natively, while others still depend on CPython. Current limitations include fixed-width integers in native execution paths, limited traceback information and incomplete native compilation coverage. I want to be transparent about these limitations rather than claim that Pyrock is a complete replacement for CPython.

Pyrock 仍处于实验阶段。某些操作可以原生编译,而其他操作仍依赖于 CPython。目前的局限性包括原生执行路径中的固定宽度整数、有限的追踪信息以及不完整的原生编译覆盖范围。我希望对这些局限性保持透明,而不是声称 Pyrock 可以完全替代 CPython。

See it in action I’ve recorded a demonstration covering native compilation, performance, standalone executable distribution and Rust/C interoperability. https://www.youtube.com/watch?v=Byc9DSE2IeQ You can explore the source code here: https://github.com/Abstrkshen/pyrock The project is open source under MIT/Apache-2.0 licensing.

查看演示:我录制了一个演示,涵盖了原生编译、性能、独立可执行文件分发以及 Rust/C 互操作性。https://www.youtube.com/watch?v=Byc9DSE2IeQ 您可以在此处探索源代码:https://github.com/Abstrkshen/pyrock 该项目在 MIT/Apache-2.0 许可下开源。

I’d love your feedback I’m particularly interested in hearing from Python developers, Rust developers and anyone working on compilers. What Python workloads would you want to compile natively? Would direct Rust and C imports be useful in your projects? How much Python compatibility would you need before considering a compiler like this? What benchmarks would you like to see? Technical criticism, suggestions and contributions are welcome. Built at Abstrkshen, an independent technology and research initiative. https://abstrkshen.com/

期待您的反馈:我特别希望能听到 Python 开发者、Rust 开发者以及任何从事编译器工作的人员的意见。您希望将哪些 Python 工作负载进行原生编译?直接导入 Rust 和 C 对您的项目有用吗?在考虑使用此类编译器之前,您需要多大程度的 Python 兼容性?您希望看到哪些基准测试?欢迎技术批评、建议和贡献。由独立技术与研究计划 Abstrkshen 构建。https://abstrkshen.com/