ScriptException when calling a python script from Java

I am calling python from Java using the ScriptEngine. The python script uses tensorflow and it runs fine if I run the python script directly. When this script is called from Java, the ScriptEngine eval() throws the following exception. It looks like something is not loaded, but am not sure what needs to be done to address it.

I am new to Python and tensorflow, so it may be trivial to fix. Appreciate your help.

javax.script.ScriptException: SyntaxError: mismatched input ‘print’ expecting NAME in pyclasspath/tensorflow/init.py at line number 288 at column number 58
at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:213)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:59)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:64)

Caused by: Traceback (most recent call last):
File “”, line 5, in
File “pyclasspath/tensorflow/init.py”, line 288
from tensorflow.python.ops.logging_ops import print_v2 as print
^
SyntaxError: mismatched input ‘print’ expecting NAME

at org.python.core.ParserFacade.fixParseError(ParserFacade.java:95)
at org.python.core.ParserFacade.parse(ParserFacade.java:190)
at org.python.core.imp.compileSource(imp.java:542)
at org.python.core.imp.compileSource(imp.java:520)
at org.python.core.util.importer.getModuleCode(importer.java:234)
at org.python.core.util.importer.importer_load_module(importer.java:106)
at org.python.core.ClasspathPyImporter.ClasspathPyImporter_load_module(ClasspathPyImporter.java:171)
at org.python.core.ClasspathPyImporter$ClasspathPyImporter_load_module_exposer.__call__(Unknown Source)
at org.python.core.PyBuiltinMethodNarrow.__call__(PyBuiltinMethodNarrow.java:48)
at org.python.core.imp.loadFromLoader(imp.java:819)
at org.python.core.imp.find_module(imp.java:763)
at org.python.core.imp.import_next(imp.java:1158)
at org.python.core.imp.import_module_level(imp.java:1350)
at org.python.core.imp.importName(imp.java:1528)
at org.python.core.ImportFunction.__call__(__builtin__.java:1285)
at org.python.core.PyObject.__call__(PyObject.java:433)
at org.python.core.__builtin__.__import__(__builtin__.java:1232)
at org.python.core.imp.importFromAs(imp.java:1620)
at org.python.core.imp.importFrom(imp.java:1595)
at org.python.pycode._pyx0.f$0(<script>:49)
at org.python.pycode._pyx0.call_function(<script>)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1687)
at org.python.core.__builtin__.eval(__builtin__.java:497)
at org.python.core.__builtin__.eval(__builtin__.java:501)
at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:255)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:57)
... 25 more

TensorFlow won’t work with Jython, you’ll need to use CPython, but we can easily use CPython from Java with JavaCPP. Here’s some sample code that you can use to get started:

Please let me know if you have any questions though!

Depending on how complex the TensorFlow script is, you might be able to implement it using TF-Java directly and avoid having a Python runtime. The JSR223 script engine is using Jython, which only supports Python 2 and so can’t run most modern Python code.