GraalVM - embedding python multi-file project in java

Viewed 204

I couldn't find a solution create a polyglot source out of multiple files in GraalVM.

What exactly I want to achieve:

I have a python project:

my-project:
  .venv/
    ...libs
  __main__.py
  src/
    __init__.py
    Service.py

Example sourcecode:

# __main__.py

from src.Service import Service

lambda url: Service(url)
# src/Service.py

import requests

class Service:

    def __init__(self, url):
        self.url = url

    def invoke(self):
        return requests.get(self.url)

This is very simple example, where we've got an entry-point script, project is structured in packages and there is one external library (requests).

It works, when I run it from command-line with python3 __main__.py, but I can't get it work, when embedding it in Java (it can't resolve imports).

Example usage in java:

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

import java.io.File;
import java.io.IOException;

public class Runner {

  public static void main(String[] args) throws IOException {
    Context context = Context.newBuilder("python")
        .allowExperimentalOptions(true)
        .allowAllAccess(true)
        .allowIO(true)
        .build();
    try (context) {
      // load lambda reference:
      Value reference = context.eval(Source.newBuilder("python", new File("/path/to/my-project/__main__.py")).build());
      // invoke lambda with `url` argument (returns `Service` object)
      Value service = reference.execute("http://google.com");
      // invoke `invoke` method of `Service` object and print response
      System.out.println("Response: " + service.getMember("invoke").execute());
    }
  }
  
}

It fails with Exception in thread "main" ModuleNotFoundError: No module named 'src'.

The solution works for javascript project (having similar index.js to __main__.py, its able to resolve imports - GraalVM "sees" other project's files, but somehow it doesn't, when using python.

I found out, that python is able to run zip package with project inside, but this also doesn't work with GraalVM.

Is there any chance to accomplish it? If not, maybe there is a similar tool to webpack for python (if I could create a single-file bundle, it should also work).

Btw, I don't know python at all, so I may missing something.

Thanks for any help!

0 Answers
Related