LinuxCommandLibrary

pypy

Run Python code with PyPy interpreter

TLDR

Start a REPL (interactive shell)

$ pypy
copy

Execute script in a given Python file
$ pypy [path/to/file.py]
copy

Execute script as part of an interactive shell
$ pypy -i [path/to/file.py]
copy

Execute a Python expression
$ pypy -c "[expression]"
copy

Run library module as a script (terminates option list)
$ pypy -m [module] [arguments]
copy

Install a package using pip
$ pypy -m pip install [package]
copy

Interactively debug a Python script
$ pypy -m pdb [path/to/file.py]
copy

SYNOPSIS

pypy [options] [-c command | -m module | script | -] [args]

PARAMETERS

-h, --help
    Display a help message and exit.

-V, --version
    Print the PyPy version number and exit.

-c command
    Execute the Python command string.

-m module
    Run the Python module as a script.

-i
    Enter interactive mode after executing script or command.

-S
    Don't import the site module on startup.

-E
    Ignore all PYTHON* environment variables.

-O
    Optimize generated bytecode, e.g., by removing assert statements.

-B
    Don't write .pyc files on import.

-u
    Force stdout, stdin, and stderr to be unbuffered binary.

-R jit-option
    PyPy specific. Controls the JIT compiler. Use -R help for options like off, verbose, trace, allowing fine-tuning of performance.

-X arg
    Set implementation-specific options for PyPy.

DESCRIPTION

PyPy is an alternative, high-performance implementation of the Python programming language, renowned for its Just-In-Time (JIT) compiler. Unlike CPython, the standard interpreter, PyPy translates Python code into machine code during execution, leading to significant speedups for many applications.

It is written in RPython, a restricted dialect of Python, allowing for self-optimization. PyPy aims for strong compatibility with CPython while focusing on speed, efficiency, and advanced features like transactional memory. It provides a robust platform for running Python applications faster, particularly CPU-bound tasks.

CAVEATS

PyPy offers significant speedups but has some caveats:
1. CPython C Extension Compatibility: While improving (via CFFI/CPyExt), not all CPython C extensions are fully compatible or performant without adaptation.
2. Startup Time: Initial startup can be slightly slower due to JIT warm-up, though this is offset by long-running process performance.
3. Memory Usage: May consume more memory initially due to JIT compiler structures.
4. Garbage Collection: Uses a different garbage collector, which behaves differently from CPython's reference counting.

JIT COMPILER

PyPy's defining feature is its Just-In-Time compiler. It dynamically analyzes frequently executed code (hot spots) during runtime, compiling it into highly optimized machine code. This eliminates the overhead of interpretation, leading to substantial performance improvements for CPU-bound applications.

RPYTHON

The RPython (Restricted Python) toolchain is a static analysis and translation framework. PyPy itself is written in RPython, which allows it to be translated into various lower-level languages like C, and crucially, enables the automatic generation of an optimizing JIT compiler tailored for the interpreter.

CFFI

CFFI stands for Foreign Function Interface for C. CFFI is PyPy's recommended way to interact with C libraries, offering a robust and performant alternative to CPython's C API, allowing Python code to call C functions and manage C data structures directly.

HISTORY

Initiated in 2003 as a research project, PyPy aimed to build a Python interpreter using RPython, a restricted subset of Python. The core idea was to enable advanced optimizations, especially Just-In-Time (JIT) compilation. The first self-hosting version was achieved in 2007, and it gained significant attention for its impressive speed gains. PyPy has since evolved, continuously improving compatibility with CPython and supporting newer Python language versions, maintaining its position as a leading alternative Python implementation.

SEE ALSO

python(1), python3(1), pip(1), virtualenv(1)

Copied to clipboard