Skip to content

Latest commit

 

History

History
92 lines (63 loc) · 5.86 KB

py_binary.md

File metadata and controls

92 lines (63 loc) · 5.86 KB

Re-implementations of py_binary and py_test

Choosing the Python version

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_binary_rule

py_binary_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_binary 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 Satisfy a virtual_dep with a mapping from external package name to the label of an installed package that provides it. See virtual dependencies. Dictionary: Label -> String optional {}
srcs Python source files. List of labels optional []

py_binary

py_binary(name, srcs, main, kwargs)

Wrapper macro for py_binary_rule.

Creates a py_venv target to constrain the interpreter and packages used at runtime. Users can bazel run [name].venv to create this virtualenv, then use it in the editor or other tools.

PARAMETERS

Name Description Default Value
name Name of the rule. none
srcs Python source files. []
main Entry point. Like rules_python, this is treated as a suffix of a file that should appear among the srcs. If absent, then [name].py is tried. As a final fallback, if the srcs has a single file, that is used as the main. None
kwargs additional named parameters to py_binary_rule. none