vmware-labs / wasm-workers-server

🚀 Develop and run serverless applications on WebAssembly

Home Page:https://workers.wasmlabs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Run Python with dependencies

denniskawurek opened this issue · comments

Describe the bug

Hi,

I wanted to run a Python module with dependencies.

When running the following code, I get an
ModuleNotFoundError: No module named 'bs4'

import bs4

def worker(req):
    res = Response(req.body)
    res.headers["x-generated-by"] = "wasm-workers-server"
    return res

I guess I need to mount my dependencies or install them in wws (?)
But I could not find any documentation about it.

Reproduction steps

  1. Store the above code as index.py
  2. Run it with wws -i
  3. Send a request to the server: http://127.0.0.1:8080

Expected behavior

No error when importing modules.

Additional context

Things I tried:

I checked the documentation, but could not find anything about adding dependencies in python based modules (only Rust).

However I tried to create a _lib directory with the bs4 dependency and mount it.

index.toml

[[folders]]
from = "./_lib"
to = "/mnt/lib"

I adjusted the import to import lib.bs4, but this didn't worked. It says that the lib module is not found.

Thanks,
Dennis

Hello @denniskawurek,

Thank you for opening the issue! You were really close as the only pending step is to configure Python to find the libraries. One approach is adding the PYTHONPATH environment variable. In your case, you only need to modify the index.toml file to set this environment variable:

name = "libs"
version = "1"

[vars]
PYTHONPATH = "/mnt/lib"

[[folders]]
from = "./_lib"
to = "/mnt/lib"

As you mentioned, there's no documentation about it so I took the opportunity to add a new example and fill this gap in the docs (#190):

Hey @Angelmmiguel ,

this worked! Thank you very much for the fast response and nice explanation.

Best
Dennis