Py2wasm: The Future of Python in WebAssembly

Py2wasm is a compiler that turns your Python code into WebAssembly, allowing you to run your Python programs at 3x faster speeds compared to CPython WASM. Learn more about this exciting new technology and how it can benefit your development workflow.
Py2wasm: The Future of Python in WebAssembly

Py2wasm: The Future of Python in WebAssembly

The world of WebAssembly (WASM) has just gotten a whole lot more interesting with the introduction of py2wasm, a compiler that turns your Python code into WebAssembly. According to the official announcement, py2wasm can run your Python programs at 3x faster speeds compared to CPython WASM (CPython interpreter inside of WebAssembly). But what does this mean, and how does it work?

WebAssembly(_download_image) WebAssembly Logo

To understand the significance of py2wasm, let’s first clarify that the 3x faster speeds are in comparison to CPython WASM, not native CPython. This is an improvement over the current state of running Python programs in WebAssembly.

Py2wasm is actually a fork of the Nuitka compiler, which compiles Python into C, but adapted to emitting WebAssembly instead. The fork exists initially as a thin layer over Nuitka. To prove this, the developers have even opened a PR on Nuitka’s Github repo.

The developers of py2wasm examined several options before deciding to use Nuitka. These options included compiling a Python subset to Wasm using CPython, RPython, and Codon; running a JIT in WebAssembly with PyPy; and static analysis to analyze/autodetect the typings of programs ahead of time, so that the code could be transpiled into more performant specializations. Candidates for this approach were mypy, mypy-c, and Nuitka.

Nuitka was chosen because it works by transpiling the Python calls that the program makes into C, using the inner CPython API calls. With a few tweaks, it has been made to emit WebAssembly executables. This means that you can run Python programs not just inside the browser but also in targets where WebAssembly WASI is supported.

“py2wasm is a thin layer over Nuitka, and we have went to the extend to open a PR on Nuitka’s Github repo.” - Wasmer

WASI, or the WebAssembly System Interface, is a modular system interface for WebAssembly (Wasm) that enables developers to run Wasm programs on any platform, including the browser, the cloud, and the edge. WASI is designed to be secure, fast, and portable, allowing developers to write their programs once and run them anywhere. In Wasmer’s case, the use case is moving their Python Django backend from Google Cloud into Wasmer Edge.

Using py2wasm is relatively straightforward. First, you need to install py2wasm using pip:

$ pip install py2wasm

Then, you can compile your Python program into WebAssembly using the following command:

$ py2wasm myprogram.py -o myprogram.wasm

Finally, you can run your WebAssembly program using Wasmer:

$ wasmer run myprogram.wasm

Before you can do this, you need to install the Wasmer runtime. On Linux, you can do this using the following command:

curl https://get.wasmer.io -sSfL | sh

On Windows, you can use the following PowerShell command:

https://win.wasmer.io -useb | iex

One caveat is that py2wasm needs to run in a Python 3.11 environment and does not (yet) support Python 3.12.

Wasmer is working to make py2wasm available as a Wasmer package to make it even easier to use.

Wasmer Logo(_download_image) Wasmer Logo

With py2wasm, the possibilities for running Python programs in WebAssembly are endless. Whether you’re a developer looking to deploy your Python application in a WebAssembly environment or simply want to explore the possibilities of WASI, py2wasm is definitely worth checking out.