graal, scala and python together note

graal, scala and python together note

If you use graal with scala and python, you can import modules but you need to do it differently.

In python, the import sys is actually a statement so it has no return value to return to graal. Instead, you need to use the “function” version:

Python 3.7.6 (default, Jan 30 2020, 09:44:41) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s  = __import__("sys")
>>> s.version
'3.7.6 (default, Jan 30 2020, 09:44:41) \n[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]'
>>> 

So in graal via ammonite, do:

@ import org.graalvm.polyglot._
@ val cs = Context.create() 
val sys_module = cs.eval("python", """__import__("sys")""")
@ sys_module.getMember("version") 
res28: Value = '3.7.4 (Mon Feb 24 18:28:41 EST 2020)\n[GraalVM CE, Java 11.0.6]'

You should define a set of implicits to provide automatic conversion, but that’s another blog. I’ll try to push mine to a easy-to-import .jar on maven so you can import them easily into your amm session.

Comments

Popular posts from this blog

quick note on scala.js, react hooks, monix, auth

zio environment and modules pattern: zio, scala.js, react, query management

attributes with react and typescript.md