Unable to convert python to wasm using pyodide compiler

356 views Asked by At

I am unable to convert python file to wasm file. tried with emscripten and pyodide for conversion from python to wasm. But was unable to get the proper compilation packages. It's not clear to get to know the right package to be used rather than Emscripten and Pyodide. Also, pyodide package was the one which didn't get installed perfectly to do compilation.

So my question is like how to convert a python code to a wasm file? What are the compilers and compilation packages used for this conversion?

2

There are 2 answers

0
rth On

As far as this question is concerned, WASM can be seen as a target architecture. So a more general question would be how to convert Python to a binary executable, with a tool that supports WASM.

Pyodide builds the CPython interpreter that runs Python code, so it doesn't do a Python to WASM conversion. You can apply Cython to your code (or mypyc) convert Python to C, and then build it to WASM with the Pyodide build system (via Emscripten).

Overall though, the other question is why do you want to do this? Python is a rather complex dynamic language, and just taking Python code and compiling it generally doesn't yield huge performance improvements, without spending lots of time tuning e.g. cython types. And even if you compile your code, you still need to ship the CPython interpreter alongside it, so size-wise it's not smaller.

There are newer projects that aim to facilitate Python-like code to wasm (for instance spy) but it's still very much experimental, and it's a Python like language, not really Python.

0
TachyonicBytes On

As far as I know, the lpython compiler is the closest compiler that resembles what you want to do.

Of course, with any such tool, you have to take into account that it can't yet compile arbitrary python code, only a specific subset. Nonetheless, it does target wasm, so in the best case, it can work for you. It's also a nice tool, so have fun experimenting with it.

There is also this project from vmware that compiles the CPython interpreter to a python.wasm file, for use with docker, exactly the case that @rth was talking about.

I am afraid the current tooling may not be enough for your use case.