Python Rules for Bazel
rules_python is the home for four major components with varying maturity levels.
Core rules
The core Python rules –py_library,py_binary,py_test,py_proto_library, and related symbols that provide the basis for Pythonsupport in Bazel.
When using Bazel 6 (or earlier), the core rules are bundled into the Bazel binary, and the symbolsin this repository are simple aliases. On Bazel 7 and above,rules_python usesa separate Starlark implementation;seeMigrating from the bundled rules below.
This repository followssemantic versioning and the breaking change policyoutlined in thesupport page.
PyPI integration
Package installation rules for integrating with PyPI and other Simple API-compatible indexes.
These rules work and can be used in production, but the cross-platform buildingthat supports pulling PyPI dependencies for a target platform that is differentfrom the host platform is still in beta, and the APIs that are subject to potentialchange are marked asexperimental.
Sphinxdocs
sphinxdocs rules allow users to generate documentation using Sphinx powered by Bazel, with additional functionality for documentingStarlark and Bazel code.
The functionality is exposed because other projects find it useful, butit is available “as is”, andthe semantic versioning andcompatibility policy used byrules_python does not apply.
Gazelle plugin
gazelle plugin for generatingBUILD.bazel files based on Python sourcecode.
This is available “as is”, and the semantic versioning used byrules_python doesnot apply.
The Bazel community maintains this repository. Neither Google nor the Bazelteam provides support for the code. However, this repository is part of thetest suite used to vet new Bazel releases. SeeHow to contribute for information on our development workflow.
Examples
This documentation is an example ofsphinxdocs rules and the rest of thecomponents have examples in theexamples directory.
Migrating from the bundled rules
The core rules are currently available in Bazel as built-in symbols, but thisform is deprecated. Instead, you should depend on rules_python in yourWORKSPACE orMODULE.bazel file and load the Python rules from@rules_python//python:<name>.bzl or load paths described in the API documentation.
Abuildifierfix is available to automatically migrateBUILD and.bzl files to add theappropriateload() statements and rewrite uses ofnative.py_*.
# Also consider using the -r flag to modify an entire workspace.buildifier--lint=fix--warnings=native-py<files>
Currently, theWORKSPACE file needs to be updated manually as perGetting started.
Note that Starlark-defined bundled symbols underneath@bazel_tools//tools/python are also deprecated. These are not yet rewrittenby buildifier.
Migrating to bzlmod
SeeBzlmod support for any behavioral differences betweenbzlmod andWORKSPACE.