//python:defs.bzl
Core rules for building Python projects.
- rule current_py_toolchain(name)
This rule exists so that the current python toolchain can be used in the
toolchainsattribute of other rules, such as genrule. It allows exposing a python toolchain after toolchain resolution has happened, to a rule which expects a concrete implementation of a toolchain, rather than a toolchain_type which could be resolved to that toolchain.
- aspect find_requirements
- Aspect Attributes:
- deps
The aspect definition. Can be invoked on the command line as
bazel build //pkg:my_py_binary_target –aspects=@rules_python//python:defs.bzl%find_requirements –output_groups=pyversioninfo
- py_binary(**attrs)
Creates an executable Python program.
This is the public macro wrapping the underlying rule. Args are forwarded on as-is unless otherwise specified. See the underlying
py_binary rulefor detailed attribute documentation.This macro affects the following args:
python_version: cannot bePY2srcs_version: cannot bePY2orPY2ONLYtags: May have special marker values added, if not already present.
- Args:
attrs– Rule attributes forwarded onto the underlyingpy_binary rule
- rule py_import(name, deps=[], srcs=[])
This rule allows the use of Python packages as dependencies.
It imports the given
.eggfile(s), which might be checked in source files, fetched externally as withhttp_file, or produced as outputs of other rules.It may be used like a
py_library, in thedepsof other Python rules.This is similar to java_import.
- Attributes:
A unique name for this target.
mandatory
deps– (list[label]) (default [])The list of other libraries to be linked in to the binary target.
optional
Required providers:
PyInfosrcs– (list[label]) (default [])The list of Python package files provided to Python targets that depend on this target. Note that currently only the .egg format is accepted. For .whl files, try the whl_library rule. We accept contributions to extend py_import to handle .whl.
optional
- py_library(**attrs)
Creates an executable Python program.
This is the public macro wrapping the underlying rule. Args are forwarded on as-is unless otherwise specified. See
py_libraryfor detailed attribute documentation.This macro affects the following args:
srcs_version: cannot bePY2orPY2ONLYtags: May have special marker values added, if not already present.
- Args:
attrs– Rule attributes forwarded ontopy_library
- py_runtime(**attrs)
Creates an executable Python program.
This is the public macro wrapping the underlying rule. Args are forwarded on as-is unless otherwise specified. See
py_runtimefor detailed attribute documentation.This macro affects the following args:
python_version: cannot bePY2srcs_version: cannot bePY2orPY2ONLYtags: May have special marker values added, if not already present.
- Args:
attrs– Rule attributes forwarded ontopy_runtime
- py_runtime_pair(name, py2_runtime=None, py3_runtime=None, **attrs)
A toolchain rule for Python.
This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3. However, Python 2 is no longer supported, so it now only wraps a single Python 3 runtime.
Usually the wrapped runtimes are declared using the
py_runtimerule, but any rule returning aPyRuntimeInfoprovider may be used.This rule returns a
platform_common.ToolchainInfoprovider with the following schema:platform_common.ToolchainInfo( py2_runtime = None, py3_runtime = <PyRuntimeInfo or None>, )
Example usage:
# In your BUILD file... load("@rules_python//python:py_runtime.bzl", "py_runtime") load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair") py_runtime( name = "my_py3_runtime", interpreter_path = "/system/python3", python_version = "PY3", ) py_runtime_pair( name = "my_py_runtime_pair", py3_runtime = ":my_py3_runtime", ) toolchain( name = "my_toolchain", target_compatible_with = <...>, toolchain = ":my_py_runtime_pair", toolchain_type = "@rules_python//python:toolchain_type", )
# In your WORKSPACE... register_toolchains("//my_pkg:my_toolchain")
- Args:
name– str, the name of the targetpy2_runtime– (default None)optional Label; must be unset or None; an error is raised otherwise.
py3_runtime– (default None)Label; a target with
PyRuntimeInfofor Python 3.attrs– Extra attrs passed onto the native rule
- py_test(**attrs)
Creates an executable Python program.
This is the public macro wrapping the underlying rule. Args are forwarded on as-is unless otherwise specified. See
py_testfor detailed attribute documentation.This macro affects the following args:
python_version: cannot bePY2srcs_version: cannot bePY2orPY2ONLYtags: May have special marker values added, if not already present.
- provider PyInfo
Encapsulates information provided by the Python rules.
- PyInfo.<init>(direct_pyc_files, has_py2_only_sources, has_py3_only_sources, imports, transitive_pyc_files, transitive_sources, uses_shared_libraries)
- PyInfo.direct_pyc_files: depset[File]
Precompiled Python files that are considered directly provided by the target.
- PyInfo.has_py2_only_sources: bool
Whether any of this target’s transitive sources requires a Python 2 runtime.
- PyInfo.has_py3_only_sources: bool
Whether any of this target’s transitive sources requires a Python 3 runtime.
- PyInfo.imports: depset[str]
A depset of import path strings to be added to the
PYTHONPATHof executable Python targets. These are accumulated from the transitivedeps. The order of the depset is not guaranteed and may be changed in the future. It is recommended to usedefaultorder (the default).
- PyInfo.transitive_pyc_files: depset[File]
Direct and transitive precompiled Python files that are provided by the target.
- PyInfo.transitive_sources: depset[File]
A (
postorder-compatible) depset of.pyfiles appearing in the target’ssrcsand thesrcsof the target’s transitivedeps.
Whether any of this target’s transitive
depshas a shared library file (such as a.sofile).This field is currently unused in Bazel and may go away in the future.
- provider PyRuntimeInfo
Contains information about a Python runtime, as returned by the
py_runtimerule.A Python runtime describes either a platform runtime or an in-build runtime. A platform runtime accesses a system-installed interpreter at a known path, whereas an in-build runtime points to a
Filethat acts as the interpreter. In both cases, an “interpreter” is really any executable binary or wrapper script that is capable of running a Python script passed on the command line, following the same conventions as the standard CPython interpreter.- PyRuntimeInfo.<init>(bootstrap_template, coverage_files, coverage_tool, files, implementation_name, interpreter, interpreter_path, interpreter_version_info, pyc_tag, python_version, stage2_bootstrap_template, stub_shebang, zip_main_template)
- PyRuntimeInfo.bootstrap_template: File
A template of code responsible for the initial startup of a program.
This code is responsible for:
Locating the target interpreter. Typically it is in runfiles, but not always.
Setting necessary environment variables, command line flags, or other configuration that can’t be modified after the interpreter starts.
Invoking the appropriate entry point. This is usually a second-stage bootstrap that performs additional setup prior to running a program’s actual entry point.
The
--bootstrap_implflag affects how this stage 1 bootstrap is expected to behave and the substutitions performed.--bootstrap_impl=system_pythonsubstitutions:%is_zipfile%,%python_binary%,%target%,%workspace_name,%coverage_tool%,%import_all%,%imports%,%main%,%shebang%--bootstrap_impl=scriptsubstititions:%is_zipfile%,%python_binary%,%target%,%workspace_name,%shebang%,%stage2_bootstrap%`
Substitution definitions:
%shebang%: The shebang to use with the bootstrap; the bootstrap template may choose to ignore this.%stage2_bootstrap%: A runfiles-relative path to the stage 2 bootstrap.%python_binary%: The path to the target Python interpreter. There are three types of paths:An absolute path to a system interpreter (e.g. begins with
/).A runfiles-relative path to an interpreter (e.g.
somerepo/bin/python3)A program to search for on PATH, i.e. a word without spaces, e.g.
python3.
%workspace_name%: The name of the workspace the target belongs to.%is_zipfile%: The string1if this template is prepended to a zipfile to create a self-executable zip file. The string0otherwise.
For the other substitution definitions, see the
stage2_bootstrap_templatedocs.Changed in version 0.33.0: The set of substitutions depends on
--bootstrap_impl
- PyRuntimeInfo.coverage_files: depset[File] | None
The files required at runtime for using
coverage_tool. Will beNoneif nocoverage_toolwas provided.
- PyRuntimeInfo.coverage_tool: File | None
If set, this field is a
Filerepresenting tool used for collecting code coverage information from python tests. Otherwise, this isNone.
- PyRuntimeInfo.files: depset[File] | None
If this is an in-build runtime, this field is a
depsetofFiles that need to be added to the runfiles of an executable target that uses this runtime (in particular, files needed byinterpreter). The value ofinterpreterneed not be included in this field. If this is a platform runtime then this field isNone.
- PyRuntimeInfo.implementation_name: str | None
The Python implementation name (
sys.implementation.name)
- PyRuntimeInfo.interpreter: File | None
If this is an in-build runtime, this field is a
Filerepresenting the interpreter. Otherwise, this isNone. Note that an in-build runtime can use either a prebuilt, checked-in interpreter or an interpreter built from source.
- PyRuntimeInfo.interpreter_path: str | None
If this is a platform runtime, this field is the absolute filesystem path to the interpreter on the target platform. Otherwise, this is
None.
- PyRuntimeInfo.interpreter_version_info: struct
Version information about the interpreter this runtime provides. It should match the format given by
sys.version_info, however for simplicity, the micro, releaselevel, and serial values are optional. A struct with the following fields:
- PyRuntimeInfo.pyc_tag: str | None
The tag portion of a pyc filename, e.g. the
cpython-39infix offoo.cpython-39.pyc. See PEP 3147. If not specified, it will be computed fromimplementation_nameandinterpreter_version_info. If no pyc_tag is available, then only source-less pyc generation will function correctly.
- PyRuntimeInfo.python_version: str
Indicates whether this runtime uses Python major version 2 or 3. Valid values are (only)
"PY2"and"PY3".
- PyRuntimeInfo.stage2_bootstrap_template: File
A template of Python code that runs under the desired interpreter and is responsible for orchestrating calling the program’s actual main code. This bootstrap is responsible for affecting the current runtime’s state, such as import paths or enabling coverage, so that, when it runs the program’s actual main code, it works properly under Bazel.
The following substitutions are made during template expansion:
%main%: A runfiles-relative path to the program’s actual main file. This can be a.pyor.pycfile, depending on precompile settings.%coverage_tool%: Runfiles-relative path to the coverage library’s entry point. If coverage is not enabled or available, an empty string.%import_all%: The stringTrueif all repositories in the runfiles should be added to sys.path. The stringFalseotherwise.%imports%: A colon-delimited string of runfiles-relative paths to add to sys.path.%target%: The name of the target this is for.%workspace_name%: The name of the workspace the target belongs to.
Added in version 0.33.0.
- PyRuntimeInfo.stub_shebang: str
“Shebang” expression prepended to the bootstrapping Python stub script used when executing
py_binarytargets. Does not apply to Windows.
- PyRuntimeInfo.zip_main_template: File
A template of Python code that becomes a zip file’s top-level
__main__.pyfile. The top-level__main__.pyfile is used when the zip file is explicitly passed to a Python interpreter. See PEP 441 for more information about zipapp support. Note that py_binary-generated zip files are self-executing and skip calling__main__.py.The following substitutions are made during template expansion:
%stage2_bootstrap%: A runfiles-relative string to the stage 2 bootstrap file.%python_binary%: The path to the target Python interpreter. There are three types of paths:An absolute path to a system interpreter (e.g. begins with
/).A runfiles-relative path to an interpreter (e.g.
somerepo/bin/python3)A program to search for on PATH, i.e. a word without spaces, e.g.
python3.
%workspace_name%: The name of the workspace for the built target.
Added in version 0.33.0.