1

I'm trying to run a polyglot native image with java/python. I'm able to create the native image with this command line:

 native-image --language:python javapython

But when a run it with ./javapython it throws me this error:

Startup failed, could not read core library from /lib-graalpython/builtins.py. Maybe you need to set python.CoreHome and python.StdLibHome.

Looking for this instruction I found this:

--python.CoreHome=<String>
--python.StdLibHome=<String>

I tried adding it with graalpython, with CoreHome it looks like it works, but with StdLibHome throws me another error:

Original exception was:

Traceback (most recent call last): File "/importlib/_bootstrap.py", line 986, in _find_and_load ModuleNotFoundError: No module named 'site'

Then run again ./javapython but it shows me the same error.

Does anyone know how to configure those paths or why this happened? Thanks

Steves
3,27023 silver badges25 bronze badges
asked Jul 10, 2020 at 19:37

1 Answer 1

3

Starting from version 23.1, all GraalVM additional languages can now be easily embedded in Java, including non-Graal JDKs (but with caveats!) by using Maven artifacts published on Mavencentral. Those artifacts embed and transparently on-demand extract the necessary files.

With regards to native-image, the languages now behave just like any other Java library that supports native-image. There are no extra options necessary for native-image, just put the relevant jars on modulepath (preferably) or classpath.

Using GraalPy in Java is now as simple as adding this to pom.xml (or equivalent code for the build system of your choice):

 <dependencies>
 <dependency>
 <groupId>org.graalvm.polyglot</groupId>
 <artifactId>polyglot</artifactId>
 <version>23.1.0</version>
 </dependency>
 <dependency>
 <groupId>org.graalvm.polyglot</groupId>
 <artifactId>python-community</artifactId>
 <version>23.1.0</version>
 <type>pom</type>
 </dependency>
 </dependencies>

then one can run Python code via GraalPy just like so:

try (var ctx = org.graalvm.polyglot.Context.create()) {
 ctx.eval("python", "print('Hello world')"); 
}

To generate native-image, one can use the Maven plugin for native-image.

More GraalPy documentation.


Old answer applicable to versions < 23.1

GraalPython needs to know where to look for its core library files and also Python standard library files. Normally, the launcher ($GRAALVM_HOME/bin/graalpython) configures this, but if you embed GraalPython in your app, you need to provide it yourself.

One possibility is to export GRAAL_PYTHONHOME pointing to $GRAALVM_HOME/jre/languages/python (on JDK11 based GraalVM builds it would be $GRAALVM_HOME/languages/python). Another is to provide all the options when building the context:

Context context = Context.newBuilder()
 .option("python.CoreHome", "/path/to/graalvm/jre/languages/python/lib-graalpython")
 .option("python.StdLibHome", "/path/to/graalvm/jre/languages/python/lib-python/3")
 // ...
 .build()
answered Jul 13, 2020 at 8:41
3
  • Thanks, it really works :). One last question, Is it the same process for Ruby with --home? Thanks Commented Jul 17, 2020 at 1:41
  • no, I don't think so, the standardization and documentation for this is actually being worked on, so once that is out, I'll update the answer. Commented Jul 18, 2020 at 23:15
  • 1
    In case it is useful, regarding Ruby question, the approach looks a bit different and driven by JVM's system properties. You actually have to set two: org.graalvm.language.ruby.home and llvm.home. In case of Java 11 -Dorg.graalvm.language.ruby.home=$GRAALVM_HOME/languages/ruby -Dllvm.home=$GRAALVM_HOME/languages/llvm. And In case of Java 8, small adjustment, -Dorg.graalvm.language.ruby.home=$GRAALVM_HOME/jre/languages/ruby -Dllvm.home=$GRAALVM_HOME/jre/languages/llvm Commented Mar 7, 2021 at 0:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.