//python/private:py_library_rule.bzl

Implementation of py_library rule.

rule py_library(name, data=[], deps=[], distribs=[], imports=[], licenses=[], precompile='inherit', precompile_invalidation_mode='auto', precompile_optimize_level=0, precompile_source_retention='inherit', srcs=[], srcs_version='PY2AND3')

A library of Python code that can be depended upon.

Default outputs:

  • The input Python sources

  • The precompiled artifacts from the sources.

NOTE: Precompilation affects which of the default outputs are included in the resulting runfiles. See the precompile-related attributes and flags for more information.

Changed in version 0.37.0: Source files are no longer added to the runfiles directly.

Attributes:
  • 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 data attribute.

    optional

    Required providers: PyInfo | CcInfo | PyInfo

  • distribs(list[str]) (default [])

    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

  • licenses(list[str]) (default [])

    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.

    • unchecked_hash: Always use the pyc file; don’t check the pyc’s hash against the source file. This is most useful when the code won’t be modified.

    For more information on pyc invalidation modes, see https://docs.python.org/3/library/py_compile.html#py_compile.PycInvalidationMode

    optional

  • precompile_optimize_level(int) (default 0)

    The optimization level for precompiled files.

    For more information about optimization levels, see the compile() function’s optimize 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:

    • inherit: Inherit the value from the --precompile_source_retention flag.

    • keep_source: Include the original Python source.

    • omit_source: Don’t include the original py source.

    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 “PY2AND3”)

    Defunct, unused, does nothing.

    optional