Run Python with GraalVM Enterprise

GraalVM Enterprise provides an implementation of Python 3.8.2. A primary goal is to support SciPy and its constituent libraries.

Installing Python and Running Python Programs

The Python engine is not available by default and should be installed.

  1. Download Oracle GraalVM Enterprise Edition Ruby Language Plugin from the Oracle Technology Network page.
  2. Having downloaded a component jar in consideration of the operating system and the underlying Java SE version, install it with:
    gu -L install component.jar
    

    For more details, check the GraalVM Updater reference.

The support for Python is still limited, but you can run simple Python scripts or commands with the graalpython binary:

graalpython [options] [-c cmd | filename]

If no program file or command is given, you are dropped into a simple REPL.

GraalVM Enterprise supports some of the same options as Python 3.8.2 and some additional options to control the underlying Python implementation, the tools and the execution engine. These can be viewed using the following command:

graalpython --help --help:tools --help:languages

Installing Supported Packages

Python comes with a tool called ginstall used to install a small list of packages known to work to some extent with GraalVM implementation of Python. It is recommended to always create a virtual environment first, using the standard Python module venv. Creating such an environment avoids any incompatible interaction with the local user’s packages that may have been installed using a system installation of CPython:

graalpython -m venv my_new_venv
source my_new_venv/bin/activate

To see the list of installable packages, run:

graalpython -m ginstall install --help

This will print a short help including a comma-separated list of packages you can install. The installation works as described in that help:

graalpython -m ginstall install pandas

Note that when using the GraalVM implementation of Python from Java, the polyglot shell, or another language, you should always evaluate the piece of Python code first to make installed packages available:

import site

Interoperability

GraalVM Enterprise provides the Python API to interact with other languages implemented with the Truffle Language Implementation framework (JavaScript, Ruby, R). In fact, GraalVM Enterprise uses this API internally to execute Python C extensions using the LLVM implementation in GraalVM.

To run the following script, pass the --jvm --polyglot options to graalpython binary. This makes other GraalVM languages available in scripts.

As a simple example, you can use the JavaScript regular expression engine to match Python strings:

import polyglot

re = polyglot.eval(string="RegExp()", language="js")

pattern = re.compile(".*(?:we have (?:a )?matching strings?(?:[!\\?] )?)(.*)")

if pattern.exec("This string does not match"):
    raise SystemError("that shouldn't happen")

md = pattern.exec("Look, we have matching strings! This string was matched by Graal.js")
if not md:
    raise SystemError("this should have matched")

print("Here is what we found: '%s'" % md[1])

If you put this code into a file polyglot_example.py, you can run it with:

graalpython --jvm --polyglot polyglot_example.py

This example matches Python strings using the JavaScript regular expression object and Python reads the captured group from the JavaScript result and prints: Here is what we found: 'This string was matched by Graal.js'.

As a more complex example, you can read a file using R, process the data in Python, and use R again to display the resulting data image, using both R and Python libraries in conjunction. To run it, first install the required R library:

R -e 'install.packages("https://www.rforge.net/src/contrib/jpeg_0.1-8.tar.gz", repos=NULL)'

This example also uses image_magix.py and works on a JPEG image input. For example, you can try with this image. These files have to be in the same folder the script below is located in and executed from.

import polyglot
import sys
import time
sys.path.insert(0, ".")
from image_magix import Image

load_jpeg = polyglot.eval(string="""function(file.name) {
    library(jpeg)
    jimg <- readJPEG(file.name)
    jimg <- jimg*255
    jimg
}""", language="R")

raw_data = load_jpeg("python_demo_picture.jpg")

# the dimensions are R attributes; define function to access them
getDim = polyglot.eval(string="function(v, pos) dim(v)[[pos]]", language="R")

# Create object of Python class 'Image' with loaded JPEG data
image = Image(getDim(raw_data, 2), getDim(raw_data, 1), raw_data)

# Run Sobel filter
result = image.sobel()

draw = polyglot.eval(string="""function(processedImgObj) {
    require(grDevices)
    require(grid)
    mx <- matrix(processedImgObj$`@data`/255, nrow=processedImgObj$`@height`, ncol=processedImgObj$`@width`)
    grDevices:::awt()
    grid.raster(mx, height=unit(nrow(mx),"points"))
}""", language="R")

draw(result)
time.sleep(10)

See the Write Polyglot Programs and the Embed Languages for more information about interoperability with other programming languages.

Tooling

Although GraalVM’s Python implementation is very early and cannot run the standard Python debugger pdb, the tools that GraalVM Enterprise provides work. To debug applications in the Chrome browser, for example, you can run a script like so:

graalpython --inspect [script-to-debug.py]
Debugger listening on port 9229.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?ws=127.0.0.1:9229/15615099-351e94d1e2db
Please note: This Python implementation is in the very early stages, and can run little more than basic benchmarks at this point.

The graalpython --help:tools command will give you more information about tools currently supported on Python.

Native Images and JVM Runtime

By default, GraalVM runs Python from an ahead-of-time compiled binary, yielding faster startup time and lower footprint. However, the ahead-of-time compiled binary only includes the Python and LLVM interpreters. To interoperate with other languages, you had to supply the --jvm argument above. This instructs the launcher to run on the JVM instead of in the Native Image mode – you will notice a longer startup time.