oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.28k stars 1.63k forks source link

ModuleNotFoundError when use GraalPython with multiple python files #5043

Open cyw3 opened 2 years ago

cyw3 commented 2 years ago

Describe the issue I use GraalPython to run multiple python files.

Steps to reproduce the issue Please include both build steps as well as run steps

  1. Here is the demo
    # test1.py
    def test1():
    print("test1")
    
    # test.py
    import test1
    import polyglot as poly

def test(): print("test") test1.test1()

poly.export_value("test", test)

test()


```java
// Test.java
import java.io.File;

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

public class Test {
    public void callPythonMethods() {
        Context ctx = Context.newBuilder().allowAllAccess(true).build();
        try {
            File fibCal = new File("./test.py");
            ctx.eval(Source.newBuilder("python", fibCal).build());

            Value hearAnalysisFn = ctx.getBindings("python").getMember("test");

            Value heartAnalysisReport = hearAnalysisFn.execute();
            System.out.println("Average age of people who are getting level 3 and greater chest pain :" + heartAnalysisReport.toString());

        }   catch (Exception e) {
            System.out.println("Exception : " );
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Test obj = new Test();
        obj.callPythonMethods();
    }
}
  1. Test with graalpython

    $ graalpython test.py
    test
    test1
  2. Test with java

    $ javac Test.java
    $ java Test
    Exception : 
    ModuleNotFoundError: No module named 'test1'
        at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:399)
        at Test.callPythonMethods(Test.java:12)
        at Test.main(Test.java:26)

Describe GraalVM and your environment:

oubidar-Abderrahim commented 2 years ago

Thank you for reporting this, we'll take a look into it shortly

oubidar-Abderrahim commented 2 years ago

Could you confirm if the issue appears in the latest release version 22.2.0?

cyw3 commented 2 years ago

version 22.2.0

The version 22.2.0 has the same issue.

oubidar-Abderrahim commented 1 year ago

Tracked internally on GR 41886

timfel commented 1 year ago

The problem is that the directory is not on the python path. When you run the commandline, the directory of the argument script is automatically put on the path. When you embed in Java, you need to do this manually. Two options:

// set PythonPath to the current working directory so we can import stuff from here
Context.newBuilder().allowAllAccess(true).option("python.PythonPath", ".") build();
# in test.py

## add the directory that this file is in to the search path
import sys
import os.path
sys.path.insert(0, os.path.dirname(__file__))

...
cyw3 commented 1 year ago

It works. Thanks.

Is there more doc proviced?