wasmerio / wasmer-js

Monorepo for Javascript WebAssembly packages by Wasmer
https://wasmerio.github.io/wasmer-js/
MIT License
936 stars 81 forks source link

[HELP] Cannot get python wasm to run with wasi #331

Open laurensV opened 1 year ago

laurensV commented 1 year ago

I am trying to run a python command with wasi from the wasmer-js package, but cannot get it to run. I want to achieve something similar to webassembly.sh functionality where I can run a python command:

image

I tried the following code:

import { init, WASI } from '@wasmer/wasi';

(async() => {
  await init();
  const command = 'python';
  const args = ["-c 'print(1)'"];
  const wasi = new WASI({
    env: {
    },
    args: [
        command, ...args
    ],
  });
  const moduleBytes = fetch('https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm');
  const module = await WebAssembly.compileStreaming(moduleBytes);
  await wasi.instantiate(module, {});

  // Run the start function
  let exitCode = wasi.start();
})()

However, I am getting the following error:

Error: Error while running start function: RuntimeError: JsValue(RuntimeError: unreachable
RuntimeError: unreachable
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:2248416)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:846043)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:843387)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:842193)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:847673)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:18244)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:16365)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:2267231)
    at null.<anonymous> (https://registry-cdn.wapm.io/contents/python/python/0.1.0/bin/python.wasm:1:15961)

Is there something I am missing here to get the python command to work?

laurensV commented 1 year ago

Actually if I wrap it in an try catch and I print wasi.getStderrString() I get the following error:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00000000 (most recent call first):
ptitSeb commented 1 year ago

you can try with python/python@0.2.0 also. I just uploaded it 2 days ago and it should be more complete than the 0.1.0 (but also heavier, at least in disk space).

ptitSeb commented 1 year ago

also, python needs many folder to run. It's not specific to the webassembly version, but also for the native version. Just running the main binary without mounting it's file system (mainly, the lib folder) will not run and lead to the error message you had.

laurensV commented 1 year ago

With python/python@0.2.0 I get the following error:

Error: Failed to instantiate WASI: RuntimeError: JsValue(TypeError: WebAssembly.Instance(): Import #43 module="wasix_32v1" error: module is not an object or function
TypeError: WebAssembly.Instance(): Import #43 module="wasix_32v1" error: module is not an object or function

@ptitSeb How would I mount the file system for this node script, is there an example somewhere? eventually I want to run this code in the browser, is that possible?

Michael-F-Bryan commented 1 year ago

hey @laurensV, the issue you are running into is that loading python.wasm alone isn't enough to start up a Python environment. You also need to give it access to the *.py files that make up the standard library, as well as a bunch of other stuff normally stored somewhere like /lib/python3.10/.

Full JavaScript support for WASIX and loading packages from the Wasmer registry is being added in #328, and there is a wasmer.sh demo being implemented in #330, but it's not quite ready for general use yet.

Michael-F-Bryan commented 10 months ago

@laurensV check out Creating an Interactive Terminal with XTerm.js for how to make a terminal like wasmer.sh using the new JavaScript SDK. You'll just need to swap the sharrattj/bash out for wasmer/python@3.12.0.