Skip to content

oracle/graalpython

Repository files navigation

GraalPy, the GraalVM Implementation of Python

Join Slack GraalVM on Twitter License

GraalPy is a high-performance implementation of the Python language for the JVM built on GraalVM. GraalPy has first-class support for embedding in Java and can turn Python applications into fast, standalone binaries.

Why GraalPy?

Low-overhead integration with Java and other languages

Compatible with the Python ecosystem

  • Install packages like NumPy, PyTorch, or Tensorflow; run Hugging Face models like Stable Diffusion or GPT
  • See if the packages you need work with our Python Compatibility Checker
  • Use almost any standard Python feature, the CPython tests run on every commit and pass ~85% We run the tests of the most depended on PyPI packages every day. For 96% of those packages a recent version can be installed on GraalPy and GraalPy passes about 50% of all tests of all packages combined. We assume that CPython not passing 100% of all tests is due to problems in our infrastructure that may also affect GraalPy. Packages where CPython fails all tests are marked as "not tested" for both CPython and GraalPy.

Runs Python code faster

  • Pure Python code is often faster than on CPython after JIT compilation
  • C extension performance is near CPython, but varies depending on the specific interactions of native and Python code
  • GraalPy is ~4x faster than CPython on the official Python Performance Benchmark Suite Benchmarks run via pip install pyperformance && pyperformance run on each of CPython and GraalPy. Harness and benchmarks were adapted by hand for Jython due to missing Python 3 support. Each interpreter was installed via pyenv. Geomean speedup was calculated against CPython on the intersection of benchmarks that run on all interpreters.

Getting Started

Embedding GraalPy in Java

GraalPy is available on Maven Central for inclusion in Java projects. Refer to our embedding documentation for more details.

  • Maven

    <dependency>
        <groupId>org.graalvm.polyglot</groupId>
        <artifactId>polyglot</artifactId>
        <version>23.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.graalvm.polyglot</groupId>
        <artifactId>python</artifactId>
        <version>23.1.2</version>
        <type>pom</type>
    </dependency>
  • Gradle

    implementation("org.graalvm.polyglot:polyglot:23.1.2")
    implementation("org.graalvm.polyglot:python:23.1.2")
Replacing CPython with GraalPy

GraalPy should in many cases work as a drop-in replacement for CPython. You can use pip to install packages as usual. Packages with C code usually do not provide binaries for GraalPy, so they will be automatically compiled during installation. This means that build tools have to be available and installation will take longer. We provide Github actions to help you build binary packages with the correct dependencies. Thanks to our integration with GraalVM Native Image, we can deploy Python applications as standalone binary, all dependencies included.

  • Linux

    The easiest way to install GraalPy on Linux is to use Pyenv (the Python version manager). To install version 23.1.2 using Pyenv, run the following commands:

    pyenv install graalpy-23.1.2
    pyenv shell graalpy-23.1.2

    Alternatively, you can download a compressed GraalPy installation file from GitHub releases.

    1. Find the download that matches the pattern graalpy-XX.Y.Z-linux-amd64.tar.gz or graalpy-XX.Y.Z-linux-aarch64.tar.gz (depending on your platform) and download.
    2. Uncompress the file and update your PATH environment variable to include the graalpy-XX.Y.Z-linux-amd64/bin (or graalpy-XX.Y.Z-linux-aarch64/bin) directory.
  • macOS

    The easiest way to install GraalPy on macOS is to use Pyenv (the Python version manager). To install version 23.1.2 using Pyenv, run the following commands:

    pyenv install graalpy-23.1.2
    pyenv shell graalpy-23.1.2

    Alternatively, you can download a compressed GraalPy installation file from GitHub releases.

    1. Find the download that matches the pattern graalpy-XX.Y.Z-macos-amd64.tar.gz or graalpy-XX.Y.Z-macos-aarch64.tar.gz (depending on your platform) and download.
    2. Remove the quarantine attribute.
      sudo xattr -r -d com.apple.quarantine /path/to/graalpy
      For example:
      sudo xattr -r -d com.apple.quarantine ~/.pyenv/versions/graalpy-23.1.2
    3. Uncompress the file and update your PATH environment variable to include to the graalpy-XX.Y.Z-macos-amd64/bin (or graalpy-XX.Y.Z-macos-aarch64/bin) directory.
  • Windows

    The Windows support of GraalPy is still experimental, so not all features and packages may be available.

    1. Find and download a compressed GraalPy installation file from GitHub releases that matches the pattern graalpy-XX.Y.Z-windows-amd64.tar.gz.
    2. Uncompress the file and update your PATH variable to include to the graalpy-XX.Y.Z-windows-amd64/bin directory.
Using GraalPy in Github Actions

The setup-python action supports GraalPy:

    - name: Setup GraalPy
      uses: actions/setup-python@v5
      with:
        python-version: graalpy # or graalpy23.1 to pin a version
Migrating Jython Scripts to GraalPy

Most existing Jython code that uses Java integration will be based on a stable Jython release—however, these are only available in Python 2.x versions. To migrate your code from Python 2 to Python 3, follow the official guide from the Python community. GraalPy provides a special mode to facilitate migration. To run Jython scripts, you need to use a GraalPy distribution running on the JVM so you can access Java classes from Python scripts.

  • Linux

    1. Find and download a compressed GraalPy installation file from GitHub releases that matches the pattern graalpy-jvm-XX.Y.Z-linux-amd64.tar.gz or graalpy-jvm-XX.Y.Z-linux-aarch64.tar.gz (depending on your platform) and download.
    2. Uncompress the file and update your PATH environment variable to include the graalpy-jvm-XX.Y.Z-linux-amd64/bin (or graalpy-jvm-XX.Y.Z-linux-aarch64/bin) directory.
    3. Run your scripts with graalpy --python.EmulateJython.
  • macOS

    1. Find and download a compressed GraalPy installation file from GitHub releases that matches the pattern graalpy-jvm-XX.Y.Z-macos-amd64.tar.gz or graalpy-jvm-XX.Y.Z-macos-aarch64.tar.gz (depending on your platform) and download.
    2. Remove the quarantine attribute.
      sudo xattr -r -d com.apple.quarantine /path/to/graalpy
      For example:
      sudo xattr -r -d com.apple.quarantine ~/.pyenv/versions/graalpy-23.1.2
    3. Uncompress the file and update your PATH environment variable to include to the graalpy-jvm-XX.Y.Z-macos-amd64/bin (or graalpy-jvm-XX.Y.Z-macos-aarch64/bin) directory.
    4. Run your scripts with graalpy --python.EmulateJython.
  • Windows

    1. Find and download a compressed GraalPy installation file from GitHub releases that matches the pattern graalpy-jvm-XX.Y.Z-windows-amd64.tar.gz.
    2. Uncompress the file and update your PATH variable to include to the graalpy-jvm-XX.Y.Z-windows-amd64/bin directory.
    3. Run your scripts with graalpy --python.EmulateJython.

Examples

Java AWT app with Python graph library using JBang  |  Standalone binary of a Python game by Joey Navarro with all dependencies included.

Documentation

GraalPy Quick Reference Sheet should help you get started. More GraalPy-specific user documentation is available in docs/user. General documentation about polyglot programming and language embedding is available on the GraalVM website.

Community

The best way to get in touch with us is to join the #graalpy channel on GraalVM Slack or tweet us.

Contributing

This project welcomes contributions from the community. Before submitting a pull request, please review our contribution guide.

If you're thinking about contributing something to this repository, you will need to sign the Oracle Contributor Agreement for us to able to merge your work. Also take a look at the code of conduct for contributors.

Security

Consult the security guide for our responsible security vulnerability disclosure process.

License

This GraalVM implementation of Python is Copyright (c) 2017, 2024 Oracle and/or its affiliates and is made available to you under the terms the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. This implementation is in part derived from and contains additional code from 3rd parties, the copyrights and licensing of which is detailed in the LICENSE and THIRD_PARTY_LICENSE files.