CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-graalvm-python--python-launcher

Command-line launcher for GraalPy, Oracle's high-performance Python implementation built on GraalVM.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

GraalPy Python Launcher

GraalPy Python Launcher is a command-line executable that provides a Python-compatible interface for running Python applications on GraalVM. It serves as the entry point for GraalPy, Oracle's high-performance Python implementation built on the JVM, offering seamless Java integration, JIT compilation, and native image capabilities.

Package Information

  • Package Name: python-launcher
  • Package Type: maven
  • Language: Java
  • Group ID: org.graalvm.python
  • Artifact ID: python-launcher
  • Installation:
    • Maven: <dependency><groupId>org.graalvm.python</groupId><artifactId>python-launcher</artifactId><version>24.2.1</version></dependency>
    • Gradle: implementation 'org.graalvm.python:python-launcher:24.2.1'
    • Typically bundled with GraalVM or GraalPy distributions as graalpy executable

Note: This artifact provides the command-line launcher. For embedding GraalPy in Java applications, use the separate org.graalvm.python:python-embedding artifact.

Core Usage

The launcher is designed to be invoked as an executable rather than used as a library. It provides a Python-compatible command-line interface:

# Basic script execution
graalpy script.py

# Interactive REPL
graalpy

# Execute code string
graalpy -c "print('Hello, World!')"

# Run module as script
graalpy -m module_name

Basic Usage

# Execute a Python script
graalpy my_script.py arg1 arg2

# Start interactive Python shell
graalpy -i

# Execute Python code directly
graalpy -c "import sys; print(sys.version)"

# Run a module with arguments
graalpy -m http.server 8080

# Verbose mode with environment ignore
graalpy -v -E script.py

Capabilities

Command-Line Execution

The launcher provides Python-compatible command-line execution with standard Python options and behavior.

public final class GraalPythonMain extends AbstractLanguageLauncher {
    /**
     * Creates a new GraalPythonMain launcher instance
     */
    public GraalPythonMain();
    
    /**
     * Main entry point for the GraalPy launcher
     * @param args Command line arguments (Python-compatible options and script/module)
     */
    public static void main(String[] args);
    
    /**
     * Preprocesses command line arguments before context creation
     * @param givenArgs Raw command line arguments
     * @param polyglotOptions Options map to populate
     * @return Processed arguments list
     */
    protected List<String> preprocessArguments(List<String> givenArgs, Map<String, String> polyglotOptions);
    
    /**
     * Validates arguments and options before execution
     * @param polyglotOptions Options map to validate
     */
    protected void validateArguments(Map<String, String> polyglotOptions);
    
    /**
     * Launches the Python context with the given configuration
     * @param contextBuilder Configured context builder
     */
    protected void launch(Context.Builder contextBuilder);
    
    /**
     * Returns the language identifier for GraalPy
     * @return "python"
     */
    protected String getLanguageId();
    
    /**
     * Returns default languages supported by this launcher
     * @return Array containing "python", "llvm", "regex"
     */
    protected String[] getDefaultLanguages();
    
    /**
     * Prints help message for command line options
     * @param maxCategory Maximum option category to display
     */
    protected void printHelp(OptionCategory maxCategory);
    
    /**
     * Collects available command line options
     * @param options Set to populate with option names
     */
    protected void collectArguments(Set<String> options);
}

Script Execution Options

File Execution:

  • graalpy script.py - Execute Python script file
  • graalpy script.py arg1 arg2 - Execute with command-line arguments

Code String Execution:

  • graalpy -c "code" - Execute Python code string
  • graalpy -c "import os; print(os.getcwd())" - Execute with imports

Module Execution:

  • graalpy -m module - Run library module as script
  • graalpy -m http.server 8080 - Run built-in HTTP server
  • graalpy -m pip install package - Run pip for package management

Interactive Mode

REPL (Read-Eval-Print Loop):

  • graalpy - Start interactive Python shell
  • graalpy -i - Force interactive mode
  • graalpy -i script.py - Interactive after script execution

Console Features:

  • Command history (when JLine is available)
  • Tab completion for Python objects and modules
  • Multi-line input support for code blocks
  • Standard Python prompts (>>> and ...)

Command-Line Options

Python Compatibility Options:

  • -c cmd - Program passed as string
  • -m mod - Run library module as script
  • -i - Inspect interactively after running script
  • -v - Verbose (trace import statements)
  • -q - Quiet (don't print version messages)
  • -E - Ignore PYTHON* environment variables
  • -B - Don't write .pyc files
  • -S - Don't imply 'import site' on initialization
  • -u - Unbuffered binary stdout and stderr
  • -I - Isolated mode (implies -E, -P, -s)
  • -P - Don't prepend potentially unsafe path to sys.path
  • -s - Don't add user site directory to sys.path
  • -V / --version - Print version and exit
  • -h / --help - Print help message
  • -W arg - Warning control
  • -X opt - Implementation-specific options

Environment Variable Support:

  • PYTHONPATH - Module search path
  • PYTHONHOME - Python installation directory
  • PYTHONIOENCODING - Encoding for stdin/stdout/stderr
  • PYTHONHASHSEED - Hash randomization seed
  • PYTHONPYCACHEPREFIX - Cache directory prefix
  • PYTHONINSPECT - Force interactive mode after script execution
  • PYTHONNOUSERSITE - Don't add user site directory to sys.path
  • PYTHONSAFEPATH - Don't prepend potentially unsafe path to sys.path
  • PYTHONVERBOSE - Verbose mode (trace import statements)
  • PYTHONUNBUFFERED - Unbuffered binary stdout and stderr
  • PYTHONDONTWRITEBYTECODE - Don't write .pyc files
  • PYTHONINTMAXSTRDIGITS - Maximum number of digits in string conversions
  • PYTHONWARNINGS - Warning filter configuration
  • PYTHONSTARTUP - File executed on interactive startup
  • PYTHONCASEOK - Ignore case in import statements (Windows)
  • GRAAL_PYTHON_ARGS - Additional launcher arguments
  • GRAAL_PYTHONHOME - GraalPy-specific Python home directory
  • VERBOSE_GRAALVM_LAUNCHERS - Enable launcher debugging output

Console Handler API

Provides console input/output handling for interactive and non-interactive execution modes.

/**
 * Abstract base class for console input/output handling
 */
abstract class ConsoleHandler {
    /**
     * Reads a line of input with optional prompting
     * @return Input line as string
     */
    public final String readLine();
    
    /**
     * Reads a line of input with prompt control
     * @param prompt Whether to display prompt
     * @return Input line as string
     */
    public abstract String readLine(boolean prompt);
    
    /**
     * Sets the prompt string for interactive input
     * @param prompt Prompt string to display
     */
    public abstract void setPrompt(String prompt);
    
    /**
     * Sets the polyglot context for the console
     * @param context Polyglot context instance
     */
    public void setContext(Context context);
    
    /**
     * Sets up readline functionality with history and completion
     * @param shouldRecord Function determining if input should be recorded
     * @param getSize Function returning history size
     * @param addItem Consumer for adding history items
     * @param getItem Function for retrieving history items by index
     * @param setItem BiConsumer for setting history items
     * @param removeItem Consumer for removing history items
     * @param clear Runnable for clearing history
     * @param completer Function providing tab completion
     */
    public void setupReader(BooleanSupplier shouldRecord, IntSupplier getSize, 
                          Consumer<String> addItem, IntFunction<String> getItem, 
                          BiConsumer<Integer, String> setItem, IntConsumer removeItem,
                          Runnable clear, Function<String, List<String>> completer);
    
    /**
     * Creates an input stream for console reading
     * @return InputStream for console input
     */
    public InputStream createInputStream();
    
    /**
     * Gets terminal width in characters
     * @return Terminal width (default: 80)
     */
    public int getTerminalWidth();
    
    /**
     * Gets terminal height in characters  
     * @return Terminal height (default: 25)
     */
    public int getTerminalHeight();
}

/**
 * JLine-based console handler with advanced terminal features
 * Provides history, tab completion, and line editing
 */
class JLineConsoleHandler extends ConsoleHandler {
    /**
     * Creates JLine console handler
     * @param inStream Input stream
     * @param outStream Output stream  
     * @param noPrompt Whether to suppress prompts
     */
    public JLineConsoleHandler(InputStream inStream, OutputStream outStream, boolean noPrompt);
}

/**
 * Basic console handler for non-interactive environments
 * Simple BufferedReader-based implementation
 */
class DefaultConsoleHandler extends ConsoleHandler {
    /**
     * Creates default console handler
     * @param in Input stream
     */
    public DefaultConsoleHandler(InputStream in);
}

GraalVM Integration Features

Polyglot Context:

  • Automatic GraalVM polyglot context creation
  • Java interoperability when running on GraalVM
  • Access to other GraalVM languages (JavaScript, Ruby, etc.)

Performance Features:

  • JIT compilation for improved performance over CPython
  • Native image support for fast startup and reduced memory footprint
  • Optimized execution of pure Python code

Virtual Environment Support:

  • Automatic detection of Python virtual environments
  • Support for pyvenv.cfg configuration files
  • Compatible with standard Python virtual environment tools

Installation and Distribution

The python-launcher is typically distributed as part of:

  1. GraalVM Community/Enterprise Edition: Bundled as the python or graalpy executable
  2. Standalone GraalPy: Available as a standalone Python distribution
  3. Maven Artifact: Can be included in Java projects for custom launcher scenarios

Error Handling

The launcher handles various error conditions:

  • Script Errors: Python exceptions are displayed with stack traces
  • Import Errors: Module not found errors with search path information
  • Syntax Errors: Python syntax errors with line numbers and context
  • Exit Codes: Proper exit code propagation from Python scripts
  • Signal Handling: Ctrl+C interrupt handling in interactive mode

Examples

# Basic script execution
graalpy hello.py

# Script with arguments
graalpy process_data.py input.csv output.csv --format json

# Interactive exploration
graalpy -i -c "import numpy as np; data = np.array([1,2,3])"

# Module execution
graalpy -m json.tool input.json

# Performance benchmarking
graalpy -X warn_default_encoding benchmark.py

# Virtual environment usage
graalpy -m venv myenv
source myenv/bin/activate  # or myenv\Scripts\activate on Windows
graalpy -m pip install requests
graalpy app.py

Types

/**
 * Required imports for using GraalPythonMain
 */
import com.oracle.graal.python.shell.GraalPythonMain;
import com.oracle.graal.python.shell.ConsoleHandler;
import com.oracle.graal.python.shell.JLineConsoleHandler;
import com.oracle.graal.python.shell.DefaultConsoleHandler;

/**
 * GraalVM/Polyglot API imports (from org.graalvm.polyglot)
 */
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Context.Builder;
import org.graalvm.options.OptionCategory;

/**
 * Standard Java imports used in API
 */
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.*;

docs

index.md

tile.json