https://github.com/emmatyping/python-wasm
Build scripts and configuration for building CPython for Emscripten
https://github.com/emmatyping/python-wasm
emscripten python wasm webassembly
Last synced: 2 months ago
JSON representation
Build scripts and configuration for building CPython for Emscripten
- Host: GitHub
- URL: https://github.com/emmatyping/python-wasm
- Owner: emmatyping
- Created: 2021-11-26T17:45:45.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-03T02:45:42.000Z (over 1 year ago)
- Last Synced: 2025-04-02T03:38:00.797Z (2 months ago)
- Topics: emscripten, python, wasm, webassembly
- Language: JavaScript
- Homepage: https://repl.ethanhs.me
- Size: 2.25 GB
- Stars: 681
- Watchers: 31
- Forks: 34
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CPython on WASM
Build scripts and configuration for building CPython for Emscripten.
Check out Christian Heimes' talk about the effort at PyConDE: https://www.youtube.com/watch?v=oa2LllRZUlU
Pretty straight forward. First, [install emscripten](https://emscripten.org/docs/getting_started/downloads.html).
Then, run the following commands:```shell
# get the Python sources
./fetch-python.sh
# build Python for the machine we are building on, needed before cross compiling for emscripten
./build-python-build.sh
# build Python cross-compiling to emscripten
./build-python-emscripten-browser.sh
```There will probably be errors, but that's just part of the fun of experimental platforms.
Assuming things compiled correctly, you can have emscripten serve the Python executable and then open http://localhost:8000/python.html in your browser:
```
./run-python-browser.sh
```The CLI input is done via an input modal which is rather annoying. Also to get output you need to click `Cancel` on the modal...
## Developing
Once you've built the Emscripten'd Python, you can rebuild it via```
./clean-host.sh
./build-python-emscripten-browser.sh
```
which will rebuild Python targeting emscripten and re-generate the `python.{html, wasm, js}`## Test build artifacts
You can also download builds from our [CI workflow](https://github.com/ethanhs/python-wasm/actions?query=branch%3Amain)
and test WASM builds locally.### Emscripten browser build
* download and unzip the ``emscripten-browser-main.zip`` build artifact
* run a local webserver in the same directory as ``python.html``,
e.g. ``python3 -m http.server``
* open http://localhost:8000/python.html
* enter commands into the browser modal window and check the web developer
console (*F12*) for output. You may need to hit "Cancel" on the modal after sending input for output to appear.### Emscripten NodeJS build
* download and unzip the ``emscripten-node-main.zip`` build artifact
* run ``node python.js`` (older versions may need ``--experimental-wasm-bigint``)### WASI
* download and unzip the ``wasi-main.zip`` build artifact
* install [wasmtime](https://wasmtime.dev/)
* run ``wasmtime run --dir . -- python.wasm``