gitmyhub

micropython-wasm

Python ★ 156 updated 14d ago

Python library for running a MicroPython sandbox using WebAssembly

This is a Python package that lets you run MicroPython, a lightweight Python variant designed for small devices, inside a locked-down WebAssembly container. The idea is to give you a safe place to execute untrusted code snippets: the sandboxed process has no access to your files or network unless you explicitly permit it. Install it from PyPI with a single pip command, then call it from the command line or import it into your own Python programs.

There are three ways to run code. The simplest is a one-shot call that starts a fresh MicroPython process, executes your snippet, captures the output, and exits. Each call is fully independent; variables and imports do not carry over between calls. For cases where you want state to persist across several calls, a session class keeps a MicroPython interpreter alive in a background thread, so variables and functions defined in one call are still available in the next. A third option rebuilds state by replaying all previous successful snippets before each new one, avoiding the background thread at the cost of slightly more work per call.

You can tune three resource limits: the maximum memory the container can use, a fuel budget that caps the total number of compute steps before the process is stopped, and a wall-clock timeout in seconds. A read-only folder from your host machine can optionally be exposed inside the container if the code needs to read input files. These controls mean that even if the code you run contains bugs or deliberate attempts to consume resources, it cannot escape the container or run indefinitely.

The project is described as experimental in the README. It uses a custom MicroPython build targeting the WASI runtime standard, which is the portable WebAssembly interface for system calls. This is distinct from the browser-targeted or Node.js-targeted MicroPython builds that exist elsewhere. The underlying execution engine is the Wasmtime Python package, which handles the WebAssembly runtime. A command-line interface is also included for running MicroPython scripts or dropping into an interactive session without writing any Python of your own.