name – (Name) A unique name for this target. mandatory
data – (list[label]) (default []) The list of files need by this library at runtime. See comments about the data attribute typically defined by rules. There is no py_embed_data like there is cc_embed_data and go_embed_data. This is because Python has a concept of runtime resources. optional
deps – (list[label]) (default []) List of additional libraries to be linked in to the target. See comments about the deps attribute typically defined by rules. These are typically py_library rules. Targets that only provide data files used at runtime belong in the dataattribute. Note The order of this list can matter because it affects the order that information from dependencies is merged in, which can be relevant depending on the ordering mode of depsets that are merged.
PyInfo.site_packages_symlinks uses topological ordering. See PyInfo for more information about the ordering of its depsets and how its fields are merged. optional Required providers:PyInfo | CcInfo
env – (dict[str, _str_]) (default {}) Dictionary of strings; optional; values are subject to $(location) and “Make variable” substitution. Specifies additional environment variables to set when the target is executed bytest or run. optional
imports – (list[str]) (default []) List of import directories to be added to the PYTHONPATH. Subject to “Make variable” substitution. These import directories will be added for this rule and all rules that depend on it (note: not the rules this rule depends on. Each directory will be added to PYTHONPATH by py_binary rules that depend on this rule. The strings are repo-runfiles-root relative, Absolute paths (paths that start with /) and paths that references a path above the execution root are not allowed and will result in an error. optional
legacy_create_init – (int) (default -1) Whether to implicitly create empty __init__.py files in the runfiles tree. These are created in every directory containing Python source code or shared libraries, and every parent directory of those directories, excluding the repo root directory. The default, -1 (auto), means true unless--incompatible_default_to_explicit_init_py is used. If false, the user is responsible for creating (possibly empty) __init__.py files and adding them to the srcs of Python targets as required. optional
main – (label) (default None) Optional; the name of the source file that is the main entry point of the application. This file must also be listed in srcs. If left unspecified,name, with .py appended, is used instead. If name does not match any filename in srcs, main must be specified. This is mutually exclusive with main_module. optional
main_module – (str) (default “”) Module name to execute as the main program. When set, srcs is not required, and it is assumed the module is provided by a dependency. See https://docs.python.org/3/using/cmdline.html#cmdoption-m for more information about running modules as the main program. This is mutually exclusive with main. Added in version 1.3.0. optional
precompile – (str) (default “inherit”) Whether py source files for this target should be precompiled. Values:
inherit: Allow the downstream binary decide if precompiled files are used.
enabled: Compile Python source files at build time.
disabled: Don’t compile Python source files at build time. See also
The --precompile flag, which can override this attribute in some cases and will affect all targets when building.
The pyc_collection attribute for transitively enabling precompiling on a per-target basis.
The Precompiling docs for a guide about using precompiling. optional
precompile_invalidation_mode – (str) (default “auto”) How precompiled files should be verified to be up-to-date with their associated source files. Possible values are:
auto: The effective value will be automatically determined by other build settings.
checked_hash: Use the pyc file if the hash of the source file matches the hash recorded in the pyc file. This is most useful when working with code that you may modify.
precompile_optimize_level – (int) (default 0) The optimization level for precompiled files. For more information about optimization levels, see the compile() function’soptimize arg docs at https://docs.python.org/3/library/functions.html#compile NOTE: The value -1 means “current interpreter”, which will be the interpreter used at build time when pycs are generated, not the interpreter used at runtime when the code actually runs. optional
precompile_source_retention – (str) (default “inherit”) Determines, when a source file is compiled, if the source file is kept in the resulting output or not. Valid values are:
include_pyc: Add implicitly generated pyc files from dependencies. i.e. pyc files for targets that specify precompile="inherit".
disabled: Don’t add implicitly generated pyc files. Note that pyc files may still come from dependencies that enable precompiling at the target level. optional
pyi_deps – (list[label]) (default []) Dependencies providing type definitions the library needs. These are dependencies that satisfy imports guarded by typing.TYPE_CHECKING. These are build-time only dependencies and not included as part of a runnable program (packaging rules may include them, however). Added in version 1.1.0. optional Required providers:PyInfo | CcInfo
pyi_srcs – (list[label]) (default []) Type definition files for the library. These are typically .pyi files, but other file types for type-checker specific formats are allowed. These files are build-time only dependencies and not included as part of a runnable program (packaging rules may include them, however). Added in version 1.1.0. optional
python_version – (str) (default “”) The Python version this target should use. The value should be in X.Y or X.Y.Z (or compatible) format. If empty or unspecified, the incoming configuration’s --python_version flag is inherited. For backwards compatibility, the values PY2 and PY3 are accepted, but treated as an empty/unspecified value. Note In order for the requested version to be used, there must be a toolchain configured to match the Python version. If there isn’t, then it may be silently ignored, or an error may occur, depending on the toolchain configuration. Changed in version 1.1.0: This attribute was changed from only accepting PY2 and PY3 values to accepting arbitrary Python versions. optional
srcs – (list[label]) (default []) The list of Python source files that are processed to create the target. This includes all your checked-in code and may include generated source files. The.py files belong in srcs and library targets belong in deps. Other binary files that may be needed at run time belong in data. optional
srcs_version – (str) (default “”) Defunct, unused, does nothing. optional
stamp – (int) (default -1) Whether to encode build information into the binary. Possible values:
stamp = 1: Always stamp the build information into the binary, even in--nostamp builds. This setting should be avoided, since it potentially kills remote caching for the binary and any downstream actions that depend on it.
stamp = 0: Always replace build information by constant values. This gives good build result caching.
stamp = -1: Embedding of build information is controlled by the--[no]stamp flag. Stamped binaries are not rebuilt unless their dependencies change. WARNING: Stamping can harm build performance by reducing cache hits and should be avoided if possible. optional