Core rules for building Python projects.

Currently the definitions here are re-exports of the native rules, "blessed" to work under --incompatible_load_python_rules_from_bzl. As the native rules get migrated to Starlark, their implementations will be moved here.

Rules

py_import

This rule allows the use of Python packages as dependencies.

It imports the given .egg file(s), which might be checked in source files, fetched externally as with http_file, or produced as outputs of other rules.

It may be used like a py_library, in the deps of other Python rules.

This is similar to java_import.

name

A unique name for this target.

deps

The list of other libraries to be linked in to the binary target.

srcs

The list of Python package files provided to Python targets that depend on this target. Note that currently only the .egg format is accepted. For .whl files, try the whl_library rule. We accept contributions to extend py_import to handle .whl.


py_runtime_pair

A toolchain rule for Python.

This wraps up to two Python runtimes, one for Python 2 and one for Python 3. The rule consuming this toolchain will choose which runtime is appropriate. Either runtime may be omitted, in which case the resulting toolchain will be unusable for building Python code using that version.

Usually the wrapped runtimes are declared using the py_runtime rule, but any rule returning a PyRuntimeInfo provider may be used.

This rule returns a platform_common.ToolchainInfo provider with the following schema:

platform_common.ToolchainInfo(
    py2_runtime = <PyRuntimeInfo or None>,
    py3_runtime = <PyRuntimeInfo or None>,
)

Example usage:

# In your BUILD file...

load("@rules_python//python:defs.bzl", "py_runtime_pair")

py_runtime(
    name = "my_py2_runtime",
    interpreter_path = "/system/python2",
    python_version = "PY2",
)

py_runtime(
    name = "my_py3_runtime",
    interpreter_path = "/system/python3",
    python_version = "PY3",
)

py_runtime_pair(
    name = "my_py_runtime_pair",
    py2_runtime = ":my_py2_runtime",
    py3_runtime = ":my_py3_runtime",
)

toolchain(
    name = "my_toolchain",
    target_compatible_with = <...>,
    toolchain = ":my_py_runtime_pair",
    toolchain_type = "@rules_python//python:toolchain_type",
)
# In your WORKSPACE...

register_toolchains("//my_pkg:my_toolchain")

name

A unique name for this target.

py2_runtime

The runtime to use for Python 2 targets. Must have python_version set to PY2.

py3_runtime

The runtime to use for Python 3 targets. Must have python_version set to PY3.


Macros and Functions

py_binary

See the Bazel core py_binary documentation.

attrs

Rule attributes


py_library

See the Bazel core py_library documentation.

attrs

Rule attributes


py_runtime

See the Bazel core py_runtime documentation.

attrs

Rule attributes


py_test

See the Bazel core py_test documentation.

attrs

Rule attributes


Aspects

find_requirements

The aspect definition. Can be invoked on the command line as

bazel build //pkg:my_py_binary_target         --aspects=@rules_python//python:defs.bzl%find_requirements         --output_groups=pyversioninfo
Propagates along attributes named: deps

name

A unique name for this target.