blob: 378aac2faa2f09d54d2f19711479a8e19bf4bdf2 [file] [log] [blame] [view]
Richard Levasseur327b4e32023-10-19 11:03:01 -07001# Python Rules for Bazel
Richard Levasseurede4fd42023-10-17 13:17:34 -07002
Ignas Anikevicius317dab42024-10-21 10:34:23 +09003`rules_python` is the home for 4 major components with varying maturity levels.
Richard Levasseur327b4e32023-10-19 11:03:01 -07004
Ignas Anikevicius317dab42024-10-21 10:34:23 +09005:::{topic} Core rules
Richard Levasseur327b4e32023-10-19 11:03:01 -07006
Ignas Anikevicius317dab42024-10-21 10:34:23 +09007The core Python rules -- `py_library`, `py_binary`, `py_test`,
8`py_proto_library`, and related symbols that provide the basis for Python
9support in Bazel.
Richard Levasseur327b4e32023-10-19 11:03:01 -070010
Ignas Anikevicius317dab42024-10-21 10:34:23 +090011When using Bazel 6 (or earlier), the core rules are bundled into the Bazel binary, and the symbols
12in this repository are simple aliases. On Bazel 7 and above `rules_python` uses
13a separate Starlark implementation,
14see {ref}`Migrating from the Bundled Rules` below.
Richard Levasseur327b4e32023-10-19 11:03:01 -070015
Ignas Anikevicius317dab42024-10-21 10:34:23 +090016Once rules_python 1.0 is released, they will follow
17[semantic versioning](https://semver.org) and the breaking change policy
18outlined in the [support](support) page.
Richard Levasseur327b4e32023-10-19 11:03:01 -070019
Ignas Anikevicius317dab42024-10-21 10:34:23 +090020:::
Richard Levasseur327b4e32023-10-19 11:03:01 -070021
Ignas Anikevicius317dab42024-10-21 10:34:23 +090022:::{topic} PyPI integration
Richard Levasseur327b4e32023-10-19 11:03:01 -070023
Ignas Anikevicius317dab42024-10-21 10:34:23 +090024Package installation rules for integrating with PyPI and other SimpleAPI
25compatible indexes.
Richard Levasseur327b4e32023-10-19 11:03:01 -070026
Ignas Anikevicius317dab42024-10-21 10:34:23 +090027These rules work and can be used in production, but the cross-platform building
28that supports pulling PyPI dependencies for a target platform that is different
29from the host platform is still in beta and the APIs that are subject to potential
30change are marked as `experimental`.
31
32:::
33
34:::{topic} Sphinxdocs
35
36`sphinxdocs` rules allow users to generate documentation using Sphinx powered by Bazel, with additional functionality for documenting
37Starlark and Bazel code.
38
39The functionality is exposed because other projects find it useful, but
40it is available as is and **the semantic versioning and
41compatibility policy used by `rules_python` does not apply**.
42
43:::
44
45:::{topic} Gazelle plugin
46
47`gazelle` plugin for generating `BUILD.bazel` files based on Python source
48code.
49
50This is available as is and the semantic versioning used by `rules_python` does
51not apply.
52
53:::
54
55The Bazel community maintains this repository. Neither Google nor the Bazel
56team provides support for the code. However, this repository is part of the
57test suite used to vet new Bazel releases. See {gh-path}`How to contribute
58<CONTRIBUTING.md>` for information on our development workflow.
59
60## Examples
61
62This documentation is an example of `sphinxdocs` rules and the rest of the
63components have examples in the {gh-path}`examples` directory.
Richard Levasseur327b4e32023-10-19 11:03:01 -070064
65## Migrating from the bundled rules
66
67The core rules are currently available in Bazel as built-in symbols, but this
68form is deprecated. Instead, you should depend on rules_python in your
Ignas Anikevicius317dab42024-10-21 10:34:23 +090069`WORKSPACE` or `MODULE.bazel` file and load the Python rules from
70`@rules_python//python:defs.bzl` or load paths described in the API documentation.
Richard Levasseur327b4e32023-10-19 11:03:01 -070071
72A [buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md)
73fix is available to automatically migrate `BUILD` and `.bzl` files to add the
74appropriate `load()` statements and rewrite uses of `native.py_*`.
75
76```sh
77# Also consider using the -r flag to modify an entire workspace.
78buildifier --lint=fix --warnings=native-py <files>
79```
80
Ignas Anikevicius317dab42024-10-21 10:34:23 +090081Currently, the `WORKSPACE` file needs to be updated manually as per
82[Getting started](getting-started).
Richard Levasseur327b4e32023-10-19 11:03:01 -070083
84Note that Starlark-defined bundled symbols underneath
85`@bazel_tools//tools/python` are also deprecated. These are not yet rewritten
86by buildifier.
87
Ignas Anikevicius317dab42024-10-21 10:34:23 +090088## Migrating to bzlmod
89
90See {gh-path}`Bzlmod support <BZLMOD_SUPPORT.md>` for any behaviour differences between
91`bzlmod` and `WORKSPACE`.
92
Richard Levasseurede4fd42023-10-17 13:17:34 -070093
94```{toctree}
Richard Levasseurede4fd42023-10-17 13:17:34 -070095:hidden:
96self
Richard Levasseur327b4e32023-10-19 11:03:01 -070097getting-started
98pypi-dependencies
Richard Levasseurb3862ec2024-09-14 22:20:01 -070099Toolchains <toolchains>
Richard Levasseur327b4e32023-10-19 11:03:01 -0700100pip
101coverage
Richard Levasseur37308032024-05-18 09:44:18 -0700102precompiling
Richard Levasseur327b4e32023-10-19 11:03:01 -0700103gazelle
Richard Levasseur847c03f2023-12-14 22:17:53 -0800104Contributing <contributing>
Richard Levasseur83e92552023-12-20 22:46:22 -0800105support
Richard Levasseur847c03f2023-12-14 22:17:53 -0800106Changelog <changelog>
Richard Levasseurede4fd42023-10-17 13:17:34 -0700107api/index
Richard Levasseur1f0765d2024-07-18 17:11:46 -0700108environment-variables
Richard Levasseur13ce1922024-08-19 11:08:38 -0700109Sphinxdocs <sphinxdocs/index>
Richard Levasseur327b4e32023-10-19 11:03:01 -0700110glossary
111genindex
Richard Levasseurede4fd42023-10-17 13:17:34 -0700112```