You loved running JavaScript in your web browser. Now, get ready for Python scripting

All thanks to CPython, WebAssembly, and some clever developers (And yes, there's Pyodide, too)


Python, one of the world's most popular programming languages, may soon become even more ubiquitous as it finds a home within web browsers.

Ethan Smith, a Berkeley-based software developer, recently revealed a project that allows CPython, the default implementation of the Python programming language, to run within web browsers via WebAssembly, or WASM.

WASM is a binary format that provides near-native performance within web browsers. It's a compilation target for languages like C/C++, C# and Rust. It's commonly used to create performance-sensitive code that JavaScript isn't well-suited to handle; wedding Python to WASM though its Emscripten compiler is more about ease of use and distribution than performance, at least at this point.

The project, created with the help of core Python developer Christian Heimes, could make Python a viable alternative to JavaScript for some web applications and could mitigate one of the lingering pain points of Python development: the installation, dependency, and virtual environment management woes memorialized in this XKCD comic that still bedevil inexperienced developers.

CPython on WASM follows in the wake of a project that debuted in April called Pyodide that similarly allows Python code to run within a web browser.

"The new project which Christian Heimes and I are working on has a goal of making the web a supported platform for CPython, just like Windows or macOS," explained Smith in a note to The Register.

"My hope is that this will enable a wider ecosystem of Python developers targeting the web, and allow for easier integration with existing Python tools and processes, many of which Pyodide has had to reinvent like micropip to replace the standard pip package installer. Eventually, I'd also like to look at WASI, the WebAssembly System Interface, as a means of sandboxing Python code server-side."

Pyodide, said Smith, has a narrow focus – running scientific computing code in the browser – and thus uses a few hacks to make packaging work properly. He sees the CPython on WASM project as having broader goals.

"I don't want this project to be quite as specific as Pyodide, but we definitely want to work with them to ensure we don't duplicate work and that scientific computing workloads work well with CPython on WASM," he explained.

"The main technical difference between our project and Pyodide is that we don't build a patched version of CPython," he said. "Instead we are upstreaming our patches, which have already made cross compiling in general much easier."

The WASM way

One of the limitations with WASM is that it doesn't support blocking/synchronous I/O operations, in order to accommodate JavaScript's non-blocking/asynchronous event loop architecture. However, Smith says, "I have plans to investigate allowing synchronous I/O to yield to the browser event loop, which as far as I know, Pyodide does not currently do. This would allow for easier integration with existing code."

Those wishing to run Python code in the browser will not immediately get access to the browser's Document Object Model, or DOM. Smith expects that's possible but will take time to implement. DOM interaction will have to be done via JavaScript until WebAssembly implements a way to talk directly to the DOM.

"I see most early usage of CPython on WASM being Javascript calling Python to execute Python code, not vice versa," he said. "There will always be places where Javascript will be a better fit than Python in the browser, but I think one of Python's best features is in its ability to act as simple to write glue code."

Smith expects JavaScript will remain the preferred language for creating user interfaces in web applications, but said he wouldn't be surprised to see a Python-based front-end framework emerge that tries to take advantage of the CPython on WASM connection.

He also said CPython on WASM could be useful for web-based cross-platform app development, though he acknowledges there's already work being done to bring CPython support to iOS and Android and there are other cross-platform projects like BeeWare.

Asked whether the WASM tie-up will make Python more competitive in terms of speed, Smith acknowledges that could be an issue.

"Our first battle to fight here is actually page load times, but we've already been able to reduce the size of the standard library to about 5 per cent of its original size, and there is more we can do," he explained.

"As for speed at runtime, I think the story will actually be the same as running on a server: if some of your code isn't fast enough, call out to C, C++, or Rust. This is where the 'glue code' aspect of Python shines. Rust in particular has great platform support for WASM and I think it would make a good pairing for accelerating Python code running on WASM."

CPython on WASM, he cautioned, is still in the early stages of development and that the project is more aspirational than functional at the moment.

"If someone wanted to start a project today, I'd most likely recommend they start with Pyodide, which has been around much longer and has more polish," he said. "But I look forward to working with the CPython and Pyodide developers to keep improving Python on WASM." ®

Broader topics


Other stories you might like

Biting the hand that feeds IT © 1998–2022