This article is more than 1 year old
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.
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.
Thanks to recent commits by @ChristianHeimes, I figured out the correct incantations to build CPython's main branch for the web!— ethantyping (@ethanhs) November 26, 2021
This is stock Python running *in the browser*.
My scripts to build this are at https://t.co/UxhHLBupj4
Lots of work to fix things though... 👷 pic.twitter.com/gIFlVNpNiX
"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
- Updated Python support in VS Code brings browser editing and ditches open-source language server for Pylance
- Credit-card-stealing, backdoored packages found in Python's PyPI library hub
- Snowflake wrestles Python, chases China, and ingests unstructured data
- About half of Python libraries in PyPI may have security issues, boffins say
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." ®