Skip to content

Compiling Python

Existing projects


Compiles a subset of (type-annotated) Python to C.

mycpp “This post is for Python experts! I briefly describe mycpp, a hacky Python-to-C++ translator I wrote on top of the MyPy type checker.”

Question: MyPy Subset with Python 3.10 Pattern Matching?

Python 3.10 was released late last year, with structural pattern matching.

One thing I found difficult when building on top of MyPy is the “inverted” visitor style. My brain doesn’t like”losing the stack” — i.e. when local variables become member variables.

I also noticed that recent versions of the AST module support type comments, which Oil still uses.

So I wonder if anybody has tried to write a strictly typed subset of MyPy with Python 3.10 pattern matching and the new ast info? I think that could be a fun project. If it’s limited to what Oil uses, it shouldn’t be too big.

Gradual typing was crucial to get Oil to where it is now, but our translator can now assume strict typing.




The PPCI (Pure Python Compiler Infrastructure) project is a compiler written entirely in the Python programming language. It contains front-ends for various programming languages as well as machine code generation functionality. With this library you can generate (working!) machine code using Python (and thus very easy to explore, extend, etc.)!

Byterun and Tailbiter

Byterun = Python VM in python (see also:
Tailbiter = compiler from Python to bytecode, in python. = fork (maintained) of byterun

Core Python = “A Python to WebAssembly compiler written in Rust.”

- Very minimal subset of the Python language. Only the core of it, nothing fancy.
- CorePython compiler itself is embeddable in browser (small WebAssembly download).

Older projects

Mython is an extensible variant of the Python programming language. Mython makes Python extensible by adding two things: parametric quotation statement, and compile-time metaprogramming. The parametric quote statement is simply syntactic sugar for saying “run some function on this embedded string”. Compile-time metaprogramming allows you to evaluate that function on the embedded string at compile time. This gives you added choice, both in terms of what your code looks like, and when you want to evaluate that code.

➜ Not sure what this really means, and if it’s useful (it was never released)
➜ There are a few research papers.
- “Language embedding and optimization in mython”

Interesting techniques / libraries

Term rewriting

Python projects:


“Improving performance of Python code using rewriting rules technique” /

“We propose an approach to increasing performance of Python code by transforming fragments of code to more efficient languages such as Cython and C++. We use high-level algebraic models and rewriting rules technique for semi-automated code transformation. Performance-critical fragments of code are transformed into a low-level syntax model using Python parser”

- (see also:

AST manipulation

Read this first:


  • ‘astpretty’: Pretty print the output of python stdlib ast.parse.
  • ‘astunparse’: An AST unparser for Python. This is a factored out version of unparse found in the Python source distribution; under Demo/parser in Python 2 and under Tools/parser in Python 3.
  • astor (previously codegen): Python AST read/write
  • Asteria (Al2O3): Missing AST features (monkey-patches ast using astor / astpretty):
  • gast: A generic AST to represent Python2 and Python3’s Abstract Syntax Tree(AST). GAST provides a compatibility layer between the AST of various Python versions, as produced by ast.parse from the standard ast module.
  • beniget: A static analyzer for Python2 and Python3 code. Beniget provides a static over-approximation of the global and local definitions inside Python Module/Class/Function. It can also compute def-use chains from each definition.

Parsers (for other things)

  • Textx


  • ‘multipledispatch’,
  • ‘jinja2’,
  • ‘numexpr’: The numexpr package supplies routines for the fast evaluation of array expressions elementwise by using a vector-based virtual machine.

Books and tutorials

Course/Book: Essentials of Compilation - An Incremental Approach in Python +
➜ Interesting tutorial / book for undergraduate students. Compiles Python to machine language. Easy to read.

“Let’s Write an LLVM Specializer for Python!” and
➜ Very interesing tutorial. But old.
➜ Hints at some additional ideas (including using and
➜ Shows some basic compilation techniques (Python AST ➜ Python Bytecode)
➜ Repo here: “Copy-and-Patch Compilation: A fast compilation algorithm for high-level languages and bytecode”
➜ Maybe interesting (or not) for us. At least, contains references to recent papers.

WASM -> Lua, mruby, grain…

Additional references


Interesting compiler in Python


See also



Page last modified: 2023-09-30 01:55:23