Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 1 | # Python Rules for Bazel |
Richard Levasseur | ede4fd4 | 2023-10-17 13:17:34 -0700 | [diff] [blame] | 2 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 3 | `rules_python` is the home for 4 major components with varying maturity levels. |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 4 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 5 | :::{topic} Core rules |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 6 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 7 | The core Python rules -- `py_library`, `py_binary`, `py_test`, |
| 8 | `py_proto_library`, and related symbols that provide the basis for Python |
| 9 | support in Bazel. |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 10 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 11 | When using Bazel 6 (or earlier), the core rules are bundled into the Bazel binary, and the symbols |
| 12 | in this repository are simple aliases. On Bazel 7 and above `rules_python` uses |
| 13 | a separate Starlark implementation, |
| 14 | see {ref}`Migrating from the Bundled Rules` below. |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 15 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 16 | Once rules_python 1.0 is released, they will follow |
| 17 | [semantic versioning](https://semver.org) and the breaking change policy |
| 18 | outlined in the [support](support) page. |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 19 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 20 | ::: |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 21 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 22 | :::{topic} PyPI integration |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 23 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 24 | Package installation rules for integrating with PyPI and other SimpleAPI |
| 25 | compatible indexes. |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 26 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 27 | These rules work and can be used in production, but the cross-platform building |
| 28 | that supports pulling PyPI dependencies for a target platform that is different |
| 29 | from the host platform is still in beta and the APIs that are subject to potential |
| 30 | change 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 |
| 37 | Starlark and Bazel code. |
| 38 | |
| 39 | The functionality is exposed because other projects find it useful, but |
| 40 | it is available as is and **the semantic versioning and |
| 41 | compatibility 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 |
| 48 | code. |
| 49 | |
| 50 | This is available as is and the semantic versioning used by `rules_python` does |
| 51 | not apply. |
| 52 | |
| 53 | ::: |
| 54 | |
| 55 | The Bazel community maintains this repository. Neither Google nor the Bazel |
| 56 | team provides support for the code. However, this repository is part of the |
| 57 | test 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 | |
| 62 | This documentation is an example of `sphinxdocs` rules and the rest of the |
| 63 | components have examples in the {gh-path}`examples` directory. |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 64 | |
| 65 | ## Migrating from the bundled rules |
| 66 | |
| 67 | The core rules are currently available in Bazel as built-in symbols, but this |
| 68 | form is deprecated. Instead, you should depend on rules_python in your |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 69 | `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 Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 71 | |
| 72 | A [buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md) |
| 73 | fix is available to automatically migrate `BUILD` and `.bzl` files to add the |
| 74 | appropriate `load()` statements and rewrite uses of `native.py_*`. |
| 75 | |
| 76 | ```sh |
| 77 | # Also consider using the -r flag to modify an entire workspace. |
| 78 | buildifier --lint=fix --warnings=native-py <files> |
| 79 | ``` |
| 80 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 81 | Currently, the `WORKSPACE` file needs to be updated manually as per |
| 82 | [Getting started](getting-started). |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 83 | |
| 84 | Note that Starlark-defined bundled symbols underneath |
| 85 | `@bazel_tools//tools/python` are also deprecated. These are not yet rewritten |
| 86 | by buildifier. |
| 87 | |
Ignas Anikevicius | 317dab4 | 2024-10-21 10:34:23 +0900 | [diff] [blame] | 88 | ## Migrating to bzlmod |
| 89 | |
| 90 | See {gh-path}`Bzlmod support <BZLMOD_SUPPORT.md>` for any behaviour differences between |
| 91 | `bzlmod` and `WORKSPACE`. |
| 92 | |
Richard Levasseur | ede4fd4 | 2023-10-17 13:17:34 -0700 | [diff] [blame] | 93 | |
| 94 | ```{toctree} |
Richard Levasseur | ede4fd4 | 2023-10-17 13:17:34 -0700 | [diff] [blame] | 95 | :hidden: |
| 96 | self |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 97 | getting-started |
| 98 | pypi-dependencies |
Richard Levasseur | b3862ec | 2024-09-14 22:20:01 -0700 | [diff] [blame] | 99 | Toolchains <toolchains> |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 100 | pip |
| 101 | coverage |
Richard Levasseur | 3730803 | 2024-05-18 09:44:18 -0700 | [diff] [blame] | 102 | precompiling |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 103 | gazelle |
Richard Levasseur | 847c03f | 2023-12-14 22:17:53 -0800 | [diff] [blame] | 104 | Contributing <contributing> |
Richard Levasseur | 83e9255 | 2023-12-20 22:46:22 -0800 | [diff] [blame] | 105 | support |
Richard Levasseur | 847c03f | 2023-12-14 22:17:53 -0800 | [diff] [blame] | 106 | Changelog <changelog> |
Richard Levasseur | ede4fd4 | 2023-10-17 13:17:34 -0700 | [diff] [blame] | 107 | api/index |
Richard Levasseur | 1f0765d | 2024-07-18 17:11:46 -0700 | [diff] [blame] | 108 | environment-variables |
Richard Levasseur | 13ce192 | 2024-08-19 11:08:38 -0700 | [diff] [blame] | 109 | Sphinxdocs <sphinxdocs/index> |
Richard Levasseur | 327b4e3 | 2023-10-19 11:03:01 -0700 | [diff] [blame] | 110 | glossary |
| 111 | genindex |
Richard Levasseur | ede4fd4 | 2023-10-17 13:17:34 -0700 | [diff] [blame] | 112 | ``` |