- Notifications
You must be signed in to change notification settings - Fork130
GraalPy – A high-performance embeddable Python 3 runtime for Java
License
oracle/graalpython
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
GraalPy is a high-performance implementation of the Python language for the JVM built onGraalVM.GraalPy is a Python 3.11 compliant runtime.It has first-class support for embedding in Java and can turn Python applications into fast, standalone binaries.GraalPy is ready for production running pure Python code and has experimental support for many popular native extension modules.
Low-overhead integration with Java and other languages
- UsePython in Java applications on GraalVM JDK, Oracle JDK, or OpenJDK
- Use JVM tools likeMaven, JFR, orGraalVM Native Image
- Manage Python libraries' system access thanks to GraalPy'sJava-based emulation of Python OS APIs
Compatible with the Python ecosystem
- Use almost any standard Python feature, the CPython tests run on every commit and pass ~85%
- See if the packages you need work according to ourPython Compatibility Checker
- Support for native extension modules is considered experimental, but you can already installpackages likeNumPy,PyTorch, orTensorflow; runHugging Face models likeStable Diffusion orGPT
We run the tests of themost depended on PyPI packages every day.For 97% of those packages a recent version can be installed on GraalPy and GraalPy passes over 60% 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 officialPython 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 viapyenv.Geomean speedup was calculated against CPython on the intersection of benchmarks that run on all interpreters.
Embedding GraalPy in Java
GraalPy isavailable on Maven Central for inclusion in Java projects.Refer to ourembedding documentation for more details.
Maven
<dependency> <groupId>org.graalvm.polyglot</groupId> <artifactId>polyglot</artifactId> <version>24.2.0</version></dependency><dependency> <groupId>org.graalvm.polyglot</groupId> <artifactId>python</artifactId> <version>24.2.0</version> <type>pom</type></dependency>
Gradle
implementation("org.graalvm.polyglot:polyglot:24.2.0")implementation("org.graalvm.polyglot:python:24.2.0")
Replacing CPython with GraalPy
GraalPy should in many cases work as a drop-in replacement for CPython.You can usepip
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 provideGithub actions to help you build binary packages with the correct dependencies.Thanks to our integration with GraalVM Native Image, we can deploy Python applications asstandalone binary, all dependencies included.
Linux
The easiest way to install GraalPy on Linux is to usePyenv (the Python version manager).To install version 24.2.0 using Pyenv, run the following commands:
pyenv install graalpy-24.2.0
pyenv shell graalpy-24.2.0
NOTE: There will be a delay between GraalPy release and its availability on Pyenv. Make sure to update Pyenv.
Alternatively, you can download a compressed GraalPy installation file fromGitHub releases.
- Find the download that matches the patterngraalpy-XX.Y.Z-linux-amd64.tar.gz orgraalpy-XX.Y.Z-linux-aarch64.tar.gz (depending on your platform) and download.
- Uncompress the file and update your
PATH
environment variable to include thegraalpy-XX.Y.Z-linux-amd64/bin (orgraalpy-XX.Y.Z-linux-aarch64/bin) directory.
macOS
The easiest way to install GraalPy on macOS is to usePyenv (the Python version manager).To install version 24.2.0 using Pyenv, run the following commands:
pyenv install graalpy-24.2.0
pyenv shell graalpy-24.2.0
NOTE: There will be a delay between GraalPy release and its availability on Pyenv. Make sure to update Pyenv.
Alternatively, you can download a compressed GraalPy installation file fromGitHub releases.
- Find the download that matches the patterngraalpy-XX.Y.Z-macos-amd64.tar.gz orgraalpy-XX.Y.Z-macos-aarch64.tar.gz (depending on your platform) and download.
- Remove the quarantine attribute.For example:
sudo xattr -r -d com.apple.quarantine /path/to/graalpy
sudo xattr -r -d com.apple.quarantine~/.pyenv/versions/graalpy-24.2.0
- Uncompress the file and update your
PATH
environment variable to include to thegraalpy-XX.Y.Z-macos-amd64/bin (orgraalpy-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.The easiest way to install GraalPy on Windows is to usePyenv-win (the Python version manager for Windows).To install version 24.2.0 using Pyenv-win, run the following commands:
pyenv install graalpy-24.2.0-windows-amd64
pyenv shell graalpy-24.2.0-windows-amd64
NOTE: There will be a delay between GraalPy release and its availability on Pyenv. Make sure to update Pyenv.
Alternatively, you can download a compressed GraalPy installation file fromGitHub releases.
- Find the download that matches the patterngraalpy-XX.Y.Z-windows-amd64.tar.gz and download.
- Uncompress the file and update your
PATH
variable to include to thegraalpy-XX.Y.Z-windows-amd64/bin directory.
Using GraalPy in Github Actions
Thesetup-python action supports GraalPy:
-name:Setup GraalPyuses:actions/setup-python@v5with:python-version:graalpy# or graalpy24.2 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, followthe official guide from the Python community.GraalPy provides aspecial 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
- Find and download a compressed GraalPy installation file fromGitHub releases that matches the patterngraalpy-jvm-XX.Y.Z-linux-amd64.tar.gz orgraalpy-jvm-XX.Y.Z-linux-aarch64.tar.gz (depending on your platform) and download.
- Uncompress the file and update your
PATH
environment variable to include thegraalpy-jvm-XX.Y.Z-linux-amd64/bin (orgraalpy-jvm-XX.Y.Z-linux-aarch64/bin) directory. - Run your scripts with
graalpy --python.EmulateJython
.
macOS
- Find and download a compressed GraalPy installation file fromGitHub releases that matches the patterngraalpy-jvm-XX.Y.Z-macos-amd64.tar.gz orgraalpy-jvm-XX.Y.Z-macos-aarch64.tar.gz (depending on your platform) and download.
- Remove the quarantine attribute.For example:
sudo xattr -r -d com.apple.quarantine /path/to/graalpy
sudo xattr -r -d com.apple.quarantine~/.pyenv/versions/graalpy-24.2.0
- Uncompress the file and update your
PATH
environment variable to include to thegraalpy-jvm-XX.Y.Z-macos-amd64/bin (orgraalpy-jvm-XX.Y.Z-macos-aarch64/bin) directory. - Run your scripts with
graalpy --python.EmulateJython
.
Windows
- Find and download a compressed GraalPy installation file fromGitHub releases that matches the patterngraalpy-jvm-XX.Y.Z-windows-amd64.tar.gz.
- Uncompress the file and update your
PATH
variable to include to thegraalpy-jvm-XX.Y.Z-windows-amd64/bin directory. - Run your scripts with
graalpy --python.EmulateJython
.
Java AWT app with Python graph library usingJBang | Standalone binary of a Python game byJoey Navarro with all dependencies included.
GraalPy Quick Reference Sheet should help you get started.More GraalPy-specific user documentation is available indocs/user.General documentation aboutpolyglot programming andlanguage embedding is available on the GraalVM website.
The best way to get in touch with us is to join the#graalpy
channel onGraalVM Slack ortweet us.
This project welcomes contributions from the community. Before submitting a pull request, pleasereview our contribution guide.
If you're thinking about contributing something to this repository, you will need to sign theOracle Contributor Agreement for us to able to merge your work.Also take a look at thecode of conduct for contributors.
Consult thesecurity guide for our responsible security vulnerability disclosure process.
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 athttps://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 theLICENSE andTHIRD_PARTY_LICENSE files.
About
GraalPy – A high-performance embeddable Python 3 runtime for Java
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.