Running Python code in a sandbox with MicroPython and WASM

· Source: Simon Willison's Weblog · Field: Technology & Digital — Software Development & Engineering, Cybersecurity & Data Privacy, Artificial Intelligence & Machine Learning · Depth: Intermediate, medium

Summary

The "micropython-wasm" alpha package, released on 6th June 2026, introduces a novel approach to running Python code in a secure sandbox environment using MicroPython compiled to WebAssembly (WASM). This solution addresses the critical need for safely executing untrusted plugin code within Python applications like Datasette, LLM, and sqlite-utils, preventing risks such as unauthorized file access, network communication, or resource exhaustion. The author leverages the "wasmtime" Python library for WASM execution and integrates MicroPython, a lean Python 3 implementation optimized for constrained environments. Key challenges overcome include maintaining persistent interpreter state across multiple code execution calls and exposing host functions, achieved through a threaded request queue and 78 lines of C code compiled into a 362KB WASM blob. The system also supports memory limits via "wasmtime" and is experimenting with a 20 million "fuel" setting for CPU limits.

Key takeaway

For AI Engineers or Software Engineers building plugin-extensible applications, "micropython-wasm" offers a promising path to secure arbitrary code execution. If you need to run untrusted Python code safely within your applications, consider experimenting with this alpha package. You can test its capabilities for memory and CPU limiting, and controlled file/network access. While currently an alpha release, exploring its integration could inform future secure plugin architectures, especially for AI agents requiring sandboxed code execution.

Key insights

Combining MicroPython with WebAssembly provides a robust, embeddable sandbox for untrusted Python code execution.

Principles

Method

Compile MicroPython to WASM, embed it via "wasmtime", manage persistent state with a threaded request queue, and expose host functions via C modules.

In practice

Topics

Code references

Best for: AI Architect, CTO, VP of Engineering/Data, Software Engineer, AI Engineer, AI Security Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Simon Willison's Weblog.