rules_py
v1.0.0
published 1 week ago
86 stars
27 forks
6 watchers
Apache License 2.0
public
17 assets
354,485 downloads
5 MB
Compatability level 1
LOSODz6q9zIEtiP5nyPUVpC4YqmUtbPCRkouNhsPxK4=
Maintained byAspect team
v1.0.0
November 13, 2024

Using Bzlmod with Bazel 6:

Add to your MODULE.bazel file:

bazel_dep(name = "aspect_rules_py", version = "1.0.0")

And also register a Python toolchain, see rules_python. For example:

# Minimum version needs:
# feat: add interpreter_version_info to py_runtime by @mattem in #1671
bazel_dep(name = "rules_python", dev_dependency = True, version = "0.29.0")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    configure_coverage_tool = True,
    python_version = "3.11",
)

Using WORKSPACE

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "aspect_rules_py",
    sha256 = "2ce48e0f3eaaf73204b623f99f23d45690b862a994b5b3c2464a2e361b0fc4ae",
    strip_prefix = "rules_py-1.0.0",
    url = "https://github.com/aspect-build/rules_py/releases/download/v1.0.0/rules_py-v1.0.0.tar.gz",
)
# Fetches the rules_py dependencies.
# If you want to have a different version of some dependency,
# you should fetch it *before* calling this.
# Alternatively, you can skip calling this function, so long as you've
# already fetched all the dependencies.
load("@aspect_rules_py//py:repositories.bzl", "rules_py_dependencies")

rules_py_dependencies()

load("@aspect_rules_py//py:toolchains.bzl", "rules_py_toolchains")

rules_py_toolchains()

# "Installation" for rules_python
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")

python_register_toolchains(
    name = "python_toolchain",
    python_version = "3.9",
)

py_repositories()

What's Changed

New Contributors

Full Changelog: https://github.com/aspect-build/rules_py/compare/v0.9.1...v1.0.0

Deps:
Assets:

Aspect's Bazel rules for Python

aspect_rules_py is a layer on top of rules_python, the standard Python ruleset hosted at https://github.com/bazelbuild/rules_python. The lower layer of rules_python is currently reused, dealing with the toolchain and dependencies.

However, this ruleset introduces a new implementation of py_library, py_binary, and py_test. Our philosophy is to behave more like idiomatic python ecosystem tools, where rules_python is closely tied to the way Google does Python development in their internal monorepo, google3. However we try to maintain compatibility with rules_python's rules for most use cases.

Layer Legacy Recommended
toolchain: fetch hermetic interpreter rules_python rules_python
pip.parse: fetch and install deps from pypi rules_python rules_python
gazelle: generate BUILD files rules_python aspect configure
rules: user-facing implementations rules_python rules_py

Watch our video series for a quick tutorial on how rules_py makes it easy to do Python with Bazel: youtube playlist

Need help? This ruleset has support provided by https://aspect.dev.

Differences

We think you'll love rules_py because it fixes many issues with rules_python's rule implementations:

Note

What about the "starlarkification" effort in rules_python?

We think this is only useful within Google, because the semantics of the rules will remain identical. Even though the code will live in bazelbuild/rules_python rather than bazelbuild/bazel, it still cannot change without breaking Google-internal usage, and has all the ergonomic bugs above due to the way the runtime is stubbed.

Installation

Follow instructions from the release you wish to use: https://github.com/aspect-build/rules_py/releases

Using with Gazelle

In any ancestor BUILD file of the Python code, add these lines to instruct Gazelle to create rules_py variants of the py_* rules:

# gazelle:map_kind py_library py_library @aspect_rules_py//py:defs.bzl
# gazelle:map_kind py_binary py_binary @aspect_rules_py//py:defs.bzl
# gazelle:map_kind py_test py_test @aspect_rules_py//py:defs.bzl

Public API

Executables

  • py_binary an executable Python program, used with bazel run or as a tool.
  • py_test a Python program that executes a test runner such as unittest or pytest, to be used with bazel test.
  • py_venv create a virtualenv for a py_binary or py_test target for use outside Bazel, such as in an editor/IDE.

Packaging

  • py_pex_binary Create a zip file containing a full Python application.

Packages

  • py_library a unit of Python code, used as a dependency of other rules.