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

Run Python with dependencies #189

Closed
denniskawurek opened this issue Jul 30, 2023 · 2 comments
Closed

Run Python with dependencies #189

denniskawurek opened this issue Jul 30, 2023 · 2 comments
Assignees
Labels
📝 documentation Improvements or additions to documentation 🔨 sdks Issues related to language SDKs

Comments

@denniskawurek
Copy link

denniskawurek commented Jul 30, 2023

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

@Angelmmiguel Angelmmiguel self-assigned this Jul 31, 2023
@Angelmmiguel Angelmmiguel added 📝 documentation Improvements or additions to documentation 🔨 sdks Issues related to language SDKs labels Jul 31, 2023
@Angelmmiguel
Copy link
Contributor

Angelmmiguel commented Jul 31, 2023

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):

@denniskawurek
Copy link
Author

Hey @Angelmmiguel ,

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

Best
Dennis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📝 documentation Improvements or additions to documentation 🔨 sdks Issues related to language SDKs
Projects
None yet
Development

No branches or pull requests

2 participants