Skip to main content
Version: 0.17.x

python

Core rules for building Python projects.

Rules

current_py_toolchain

This rule exists so that the current python toolchain can be used in the toolchains attribute of other rules, such as genrule. It allows exposing a python toolchain after toolchain resolution has happened, to a rule which expects a concrete implementation of a toolchain, rather than a toolchain_type which could be resolved to that toolchain.

Example usage (generated):

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

current_py_toolchain(
# A unique name for this target.
name = "",
)

name

Required name.

A unique name for this target.

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.

Example usage (generated):

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

py_import(
# A unique name for this target.
name = "",
)

name

Required name.

A unique name for this target.

deps

Optional list of labels. Default: []

[Must provide PyInfo]

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

srcs

Optional list of labels. Default: []

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.

Macros and Functions

py_binary

See the Bazel core py_binary documentation.

Example usage (generated):

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

py_binary(
)

attrs

Optional.

Rule attributes

py_library

See the Bazel core py_library documentation.

Example usage (generated):

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

py_library(
)

attrs

Optional.

Rule attributes

py_runtime

See the Bazel core py_runtime documentation.

Example usage (generated):

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

py_runtime(
)

attrs

Optional.

Rule attributes

py_runtime_pair

A toolchain rule for Python.

This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3. However, Python 2 is no longer supported, so it now only wraps a single Python 3 runtime.

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 = 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_py3_runtime",
interpreter_path = "/system/python3",
python_version = "PY3",
)

py_runtime_pair(
name = "my_py_runtime_pair",
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

Required.

str, the name of the target

py2_runtime

Optional. Default: None

optional Label; must be unset or None; an error is raised otherwise.

py3_runtime

Optional. Default: None

Label; a target with PyRuntimeInfo for Python 3.

attrs

Optional.

Extra attrs passed onto the native rule

py_test

See the Bazel core py_test documentation.

Example usage (generated):

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

py_test(
)

attrs

Optional.

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

Required name

A unique name for this target.