Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
laurensV opened this issue Oct 5, 2023 · 6 comments
Open

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

laurensV opened this issue Oct 5, 2023 · 6 comments

Comments

@laurensV
Copy link

laurensV commented Oct 5, 2023

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
Copy link
Author

laurensV commented Oct 5, 2023

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
Copy link

ptitSeb commented Oct 5, 2023

you can try with python/[email protected] 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
Copy link

ptitSeb commented Oct 5, 2023

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
Copy link
Author

laurensV commented Oct 5, 2023

With python/[email protected] 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
Copy link
Contributor

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
Copy link
Contributor

@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/[email protected].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants