Rye

repository·main·Indexed 12 days ago

https://github.com/astral-sh/rye

A unified Python project and package management tool that handles Python installation, dependency management, virtualenvs, and linting/formatting in a single workflow. Version 0.44.0. Note: Rye is no longer actively developed; users are encouraged to migrate to uv.

Tokens
41.7K
Snippets
185
Records
228
Agent score
93%

What's inside Rye

  1. What is Rye?

    main

    Rye is an all-in-one tool for Python development designed to simplify the developer experience. Instead of managing multiple tools, Rye acts as a single entry point that handles:

    • Python Installation: Rye automatically installs Python interpreters so you don't need to install Python manually.
    • Package Management: It installs packages from various package indexes.
    • Environment Management: It manages virtual environments (virtualenvs) behind the scenes.

    The goal is to provide a unified workflow where the user only needs to interact with Rye to manage their entire Python stack.

  2. What is Rye and what does it include?

    main

    Rye is a comprehensive project and package management solution for Python designed to provide a unified experience for managing Python installations, pyproject.toml based projects, dependencies, and virtualenvs. It supports complex projects, monorepos, and global tool installations.

    Core Features and Tooling

    • Python Bootstrapping: Automatically provides access to Indygreg Python Builds and PyPy binary distributions.
    • Linting and Formatting: Bundles ruff, accessible via rye lint and rye fmt.
    • Virtualenv Management: Uses the virtualenv library.
    • Wheel Building: Delegates to build.
    • Publishing: Uses twine for the publish command.
    • Locking and Dependency Installation: Uses uv (primary) with fallbacks to unearth and pip-tools.
    • Workspace Support: Enables management of complex projects consisting of multiple libraries.
  3. Understand the Rye vision and philosophy

    main

    Rye aims to bring a Rust-like development experience to Python. This involves moving away from system-installed Python distributions and toward a model where a single tool manages the entire toolchain, including Python versions, dependency resolution, and project structure.

    Key pillars of this vision include:

    • Bootstrapping Python: Rye prefers providing its own standardized Python builds rather than relying on system-installed versions to avoid inconsistencies caused by Linux distribution patches.
    • Stronger Resolver: A resolver that supports environment markers (cross-platform resolution), multi-version resolution (allowing different parts of the dependency tree to use different versions of the same package), and a policy layer for supply chain safety.
    • Lockfiles: Support for pragmatic lockfiles that cover multiple platforms (e.g., Windows and Linux) simultaneously.
    • Workspaces: Support for managing multiple related projects that share a single virtual environment and dependency resolution.
    • Opinionated Defaults: Encouraging standardization by providing preferred folder structures and integrated linting/formatting tools.
  4. How Rye manages Python distributions

    main
    Rye's philosophy is to bootstrap Python independently. Instead of picking up a system-installed Python distribution (which can lead to frustration due to OS-specific patches), Rye aims to ensure that users get a consistent Python distribution via Rye itself. This approach provides reliable, simple rules that do not differ between systems and facilitates easier cross-version testing in CI or via tools like tox.
  5. Efficient Indexing via Meta Server RESTful API

    main

    A Meta Server is intended to replace slow HTML parsing with an efficient RESTful API. The primary lookup operations for a package manager are:

    1. Canonical Name Lookup: Resolving a package name (e.g., "foo") to its registered canonical name (e.g., "Foo").
    2. Version Discovery: Finding all published versions of a specific package.
    3. Metadata Discovery: Retrieving resolver-relevant metadata. This includes both the original metadata stored in the wheel and any patched metadata applied by the server.

    By exposing metadata for packages built from source, the Meta Server allows resolvers to function without needing to build source packages during the resolution phase.

  6. How Rye handles virtual environments

    main
    Rye treats virtual environments as managed, throw-away scratch-pads for dependencies. The goal is to have a consistent path for the virtual environment (e.g., .venv in the workspace root) so that users do not need to interact with it manually. Rye takes full responsibility for its creation and management, aiming to flatten the structure across different platforms and Python versions.
  7. Use Rye alongside other Python installations

    main
    Rye is designed to coexist with other Python installations. Even if Rye shims are first in your PATH, Rye will automatically detect and use a different Python installation if you are working in a directory that contains a non-Rye managed project.
  8. How Rye shims work

    main

    Rye installs two shims, python and python3, onto your PATH. These shims behave differently based on your current working directory:

    1. Inside a Rye managed project: The shims automatically resolve to the Python interpreter of the project's virtualenv. This allows you to run python in your shell without manually activating the virtual environment; it will automatically operate within the correct context.
    2. Outside a Rye managed project: By default, the shims resolve to your system Python to avoid disrupting existing workflows. However, you can configure them to resolve to a Rye-managed Python installation instead.
  9. Concept: What is a Meta Server (metasrv)?

    main

    A Meta Server (metasrv) is an auxiliary system designed to sit in front of a source repository (like PyPI or an S3 bucket) to improve the efficiency and reliability of package management.

    Instead of a package manager (like Rye) pointing directly to a massive HTML-based index (e.g., https://pypi.org/simple/), it points to a Meta Server URL (e.g., https://meta.pypi.org/). The Meta Server is responsible for surfacing packages from the underlying source repository through a more efficient interface.

    Key Responsibilities:

    • Efficient Indexing: Provides a RESTful API for fast lookups of package names, versions, and resolver-relevant metadata.
    • Metadata Caching: Caches original metadata from source archives and wheels.
    • Metadata Patching: Allows for overriding dependency information (like upper bounds) to improve resolver compatibility.
    • Trusted Writes: Acts as an arbiter for "understandings" (sets of metadata overrides) provided by trusted parties.
    • Marker Management: Maintains lists of well-known environment marker values.
  10. Understand the behavior and limitations of virtual projects

    main

    When using virtual projects in Rye, be aware of the following constraints:

    • Installation Behavior: The project itself is never installed into the virtual environment because it is not considered a valid package.
    • Publishing: You cannot publish virtual packages to PyPI or any other index.
    • Optional Dependencies: Virtual projects cannot have optional dependencies. Even if declared in pyproject.toml, they will not be installed.
    • Workspaces: If a Rye workspace does not contain a top-level package, it is recommended to declare that workspace as virtual.
  11. How Meta Servers handle metadata patching and 'understandings'

    main

    A Meta Server can provide Patched Meta Data to help resolvers handle packages that are practically compatible even if their stated version ranges suggest otherwise (e.g., overriding restrictive upper bounds).

    Because these patches are often specific to certain ecosystems or groups of users, they are organized into understandings. An "understanding" is a registered set of metadata overrides.

    Workflow for contributing patches:

    1. A tool or user wants to publish an override for a specific package and version.
    2. The tool requests the destination for the write from the Meta Server, specifying an --understanding.
    3. The Meta Server directs the tool to a specific location (e.g., a Git repository) where that metadata should be stored.
    4. Users opt-into one or more understandings via their package manager to receive these patched metadata entries during resolution.

    Example conceptual command:

    tool upload-override --package Flask --version 2.0 --file metadata.json --understanding pallets
  12. Configure TKinter Support for macOS and Linux

    main

    On macOS and Linux, the portable Python builds used by Rye may fail to find TCL/TK runtime files within virtualenvs. Newer versions of Rye attempt to handle this automatically by exporting TCL_LIBRARY and TK_LIBRARY.

    If you need to manually set these in your environment, they should point to the library paths within your sys.base_prefix.

    import os
    import sys
    os.environ["TCL_LIBRARY"] = sys.base_prefix + "/lib/tcl8.6"
    os.environ["TK_LIBRARY"] = sys.base_prefix + "/lib/tk8.6"