Re-implementations of py_binary and py_test
The python_version
attribute must refer to a python toolchain version
which has been registered in the WORKSPACE or MODULE.bazel file.
When using WORKSPACE, this may look like this:
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
python_register_toolchains(
name = "python_toolchain_3_8",
python_version = "3.8.12",
# setting set_python_version_constraint makes it so that only matches py_* rule
# which has this exact version set in the `python_version` attribute.
set_python_version_constraint = True,
)
# It's important to register the default toolchain last it will match any py_* target.
python_register_toolchains(
name = "python_toolchain",
python_version = "3.9",
)
Configuring for MODULE.bazel may look like this:
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.8.12", is_default = False)
python.toolchain(python_version = "3.9", is_default = True)
py_test_rule(name, data, deps, env, imports, main, package_collisions, python_version, resolutions, srcs)
Run a Python program under Bazel. Most users should use the py_test macro instead of loading this directly.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
data | Runtime dependencies of the program. The transitive closure of the data dependencies will be available in the .runfiles folder for this binary/test. The program may optionally use the Runfiles lookup library to locate the data files, see https://pypi.org/project/bazel-runfiles/. |
List of labels | optional | [] |
deps | Targets that produce Python code, commonly py_library rules. |
List of labels | optional | [] |
env | Environment variables to set when running the binary. | Dictionary: String -> String | optional | {} |
imports | List of import directories to be added to the PYTHONPATH. | List of strings | optional | [] |
main | Script to execute with the Python interpreter. | Label | required | |
package_collisions | The action that should be taken when a symlink collision is encountered when creating the venv. A collision can occour when multiple packages providing the same file are installed into the venv. The possible values are: * "error": When conflicting symlinks are found, an error is reported and venv creation halts. * "warning": When conflicting symlinks are found, an warning is reported, however venv creation continues. * "ignore": When conflicting symlinks are found, no message is reported and venv creation continues. |
String | optional | "error" |
python_version | Whether to build this target and its transitive deps for a specific python version. | String | optional | "" |
resolutions | FIXME | Dictionary: Label -> String | optional | {} |
srcs | Python source files. | List of labels | optional | [] |
py_pytest_main(name, py_library, deps, data, testonly, kwargs)
py_pytest_main wraps the template rendering target and the final py_library.
PARAMETERS
py_test(name, main, srcs, kwargs)
Identical to py_binary, but produces a target that can be used with bazel test
.
PARAMETERS
Name | Description | Default Value |
---|---|---|
name | - |
none |
main | - |
None |
srcs | - |
[] |
kwargs | - |
none |