Skip to main content

Python SDK

RBS provides first-class Python support with hermetic toolchains, automatic dependency resolution, test coverage enforcement, and linting — all without requiring pip, venv, or any system-installed Python.

Setup

Toolchain

Register a Python toolchain in your WORKSPACE.rbs. RBS automatically downloads and manages the interpreter.
load("@rbs//python/toolchain.rbs", "python_toolchain")

python_toolchain(name = "python3", version = "3.12")

Dependencies

Define external packages using py_repository. RBS resolves and caches them hermetically.
load("@rbs//python/dependencies.rbs", "py_repository")

py_repository(name = "django_repo", package = "django", version = "5.0.1")
py_repository(name = "requests_repo", package = "requests", version = "2.32.4")
py_repository(name = "gunicorn_repo", package = "gunicorn", version = "23.0.0")

Linting

Register Python linters (Black, isort, Flake8, Ruff) for workspace-wide use:
load("@rbs//python/lint.rbs", "register_python_linters")
register_python_linters()

Rules

py_library

Creates a reusable Python library that other targets can depend on.
load("@rbs//python/rules.rbs", "py_library")

py_library(
    name = "calculator",
    srcs = ["src/calculator.py"],
)

py_library(
    name = "string_utils",
    srcs = ["src/string_utils.py"],
)
AttributeTypeDescription
namestringUnique target name (required).
srcslistPython source files.
depslistDependencies (local targets or @external:// references).

py_binary

Creates an executable Python application with all dependencies bundled.
load("@rbs//python/rules.rbs", "py_binary")

py_binary(
    name = "app",
    srcs = ["src/main.py"],
    main = "src/main.py",
    deps = [":calculator", ":string_utils"],
)
AttributeTypeDescription
namestringUnique target name (required).
srcslistPython source files.
mainstringEntry point file (required).
depslistDependencies.
argslistDefault arguments passed to the binary.
envdictEnvironment variables.

py_test

Runs Python tests with optional coverage thresholds. Pytest is auto-included — no manual dependency needed.
load("@rbs//python/rules.rbs", "py_test")

# Basic test
py_test(
    name = "calculator_test",
    srcs = ["tests/test_calculator.py"],
    deps = [":calculator"],
)

# Test with coverage enforcement — FAILS if below threshold
py_test(
    name = "calculator_coverage_test",
    srcs = ["tests/test_calculator.py"],
    deps = [":calculator"],
    min_line_coverage = 85,
    min_branch_coverage = 75,
    min_function_coverage = 80,
    size = "small",
    timeout = 60,
)
AttributeTypeDescription
namestringUnique target name (required).
srcslistTest source files.
depslistDependencies (libraries under test).
test_runnerstring"pytest" (default) or "unittest".
min_line_coverageintMinimum line coverage percentage. Test fails if below.
min_branch_coverageintMinimum branch coverage percentage.
min_function_coverageintMinimum function coverage percentage.
sizestringTest size ("small", "medium", "large").
timeoutintTimeout in seconds.

py_lint

Runs linters (Black + isort by default) on Python source files.
load("@rbs//python/lint.rbs", "py_lint")

py_lint(
    name = "lint",
    srcs = glob(["**/*.py"], exclude = [".rbs/**", "__pycache__/**"]),
)

# Auto-fix mode
py_lint(
    name = "lint_fix",
    srcs = glob(["**/*.py"], exclude = [".rbs/**", "__pycache__/**"]),
    fix = True,
)
AttributeTypeDescription
namestringUnique target name (required).
srcslistSource files to lint.
fixboolAuto-fix lint issues (default False).

Example: Django Blog

A complete Django application with development/production servers, management commands, and linting.
load("@rbs//python/toolchain.rbs", "python_toolchain")
load("@rbs//python/lint.rbs", "register_python_linters")
load("@rbs//python/dependencies.rbs", "py_repository")

python_toolchain(name = "python3", version = "3.12")
register_python_linters()

py_repository(name = "django_repo", package = "django", version = "5.0.1")
py_repository(name = "gunicorn_repo", package = "gunicorn", version = "23.0.0")
py_repository(name = "whitenoise_repo", package = "whitenoise", version = "6.6.0")
py_repository(name = "pillow_repo", package = "pillow", version = "10.2.0")
py_repository(name = "requests_repo", package = "requests", version = "2.32.4")

Commands

rbs build app                          # Build binary
rbs run dev_server                     # Run development server
rbs test calculator_test               # Run tests
rbs test calculator_coverage_test      # Run tests with coverage enforcement
rbs coverage calculator_test           # Generate coverage report
rbs build lint                         # Run linter