Python Quick Start

GraalVM is not just a Java Virtual Machine to run Java. It is also a high-performance multilingual runtime and provides support for a number of languages beyond Java, allowing different languages and libraries to interoperate with no performance penalty.

Python is one of the supported languages and GraalVM provides the Python 3 runtime environment under the code name GraalPy. The key to GraalVM’s polyglot support is language compliance, and a primary goal of the GraalVM Python runtime is to support SciPy and its constituent libraries, to work with other data science and machine learning libraries from the rich Python ecosystem.

The Python runtime, GraalPy, is currently experimental but it already offers performance 5-6 times faster than CPython 3.8 (after warm-up) or 6-7x faster than Jython. Apart from the performance benefits, GraalVM’s Python runtime enables support for native extensions that Jython has never supported, the possibility to create native platform binaries using the Native Image, a managed execution mode to run, for example, NumPy extensions in a safe manner, and many more.

Here you will find information about installing GraalVM and Python support, running Python applications, creating a virtual environment, and adding supported packages. If you have experience using GraalVM’s Python runtime, you can skip this getting started guide and proceed to the in-depth Python reference manual.

Install GraalVM

To run Python on GraalVM, you need to have GraalVM installed first. Choose the platform and proceed to the installation steps:

Install Python

The Python runtime is not shipped with the default GraalVM installation package, but can be quickly added using the updater utility, gu:

gu install python

The Python runtime requires the LLVM toolchain for its operation, and you will be prompted to install this dependency, if not yet installed:

gu install llvm-toolchain

Once Python’s runtime is installed, the graalpy binary appears in the JAVA_HOME/bin/ directory.

Run Python Applications

GraalVM’s Python support targets Python 3.8 compatibility. The support is still limited at this stage, but you can run simple Python commands or programs with the graalpy launcher:

graalpy [options] [-c cmd | filename]

If no program file or command is given, you will be dropped into the Python shell and run Python scripts interactively:

>>> 1 + 2
>>> exit()

GraalVM supports some of the same options as Python 3.8 as well as some additional options to control the underlying Python runtime, GraalVM’s tools, and the execution engine. These can be viewed using the following command:

graalpy --help --help:tools --help:languages

Create a Virtual Environment and Install Supported Packages

The recommended way of using GraalVM’s Python runtime is from a virtual environment. Creating a virtual environment prevents any incompatible interaction with the local user’s packages that may have been installed using a system installation of CPython.

  1. Create the virtual environment with GraalVM:
    graalpy -m venv <venv-dir>
  2. Activate the environment in the shell session:
    source <venv-dir>/bin/activate

There are not enough standard libraries to run the standard package installers for many packages with GraalVM Python runtime yet. As a convenience, GraalVM’s Python runtime provides a tool called ginstall to install the available packages.

To see the list of installable packages, run:

graalpy -m ginstall install --help

To install the package, provide the name:

graalpy -m ginstall install <package name>

For more information, continue reading to the Installing Supported Packages guide.

For more extensive user documentation, proceed to the Python reference manual. If you consider GraalVM’s Python runtime as a replacement for Jython, consult the Jython Migration guide. The code base of GraalPy is available on GitHub.

Connect with us