Rules
py_binary
View rule sourcepy_binary(name, deps, srcs, data, args, compatible_with, deprecation, distribs, env, exec_compatible_with, exec_properties, features, imports, legacy_create_init, licenses, main, output_licenses, python_version, restricted_to, srcs_version, stamp, tags, target_compatible_with, testonly, toolchains, visibility)
  A py_binary is an executable Python program consisting
  of a collection of .py source files (possibly belonging
  to other py_library rules), a *.runfiles
  directory tree containing all the code and data needed by the
  program at run-time, and a stub script that starts up the program with
  the correct initial environment and data.
Examples
py_binary(
    name = "foo",
    srcs = ["foo.py"],
    data = [":transform"],  # a cc_binary which we invoke at run time
    deps = [
        ":foolib",  # a py_library
    ],
)
If you want to run a py_binary from within another binary or
   test (for example, running a python binary to set up some mock resource from
   within a java_test) then the correct approach is to make the other binary or
   test depend on the py_binary in its data section. The other
   binary can then locate the py_binary relative to the source
   directory.
py_binary(
    name = "test_main",
    srcs = ["test_main.py"],
    deps = [":testing"],
)
java_library(
    name = "testing",
    srcs = glob(["*.java"]),
    data = [":test_main"]
)
Arguments
| Attributes | |
|---|---|
| name | Name; required A unique name for this target. If mainis unspecified, this should be the same as the name
    of the source file that is the main entry point of the application,
    minus the extension.  For example, if your entry point is calledmain.py, then your name should bemain. | 
| deps | List of labels; default is  depsat
          
          Typical attributes defined by most build rules.
          These are generallypy_libraryrules. | 
| srcs | List of labels; requiredThe list of source ( .py) files that are processed to create the target.
          This includes all your checked-in code and any generated source files. Library targets
          belong indepsinstead, while other binary files needed at runtime belong indata. | 
| imports | List of strings; default is  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  
          Absolute paths (paths that start with  | 
| legacy_create_init | Integer; default is  --incompatible_default_to_explicit_init_pyis used. If false, the user is
          responsible for creating (possibly empty) __init__.py files and adding them to thesrcsof Python targets as required. | 
| main | Label; default is  srcs. If left unspecified,nameis used instead (see above). Ifnamedoes not
          match any filename insrcs,mainmust be specified. | 
| python_version | String; nonconfigurable; default is  deps) for Python 2 or Python
          3. Valid values are"PY2"and"PY3"(the default).The Python version is always reset (possibly by default) to whatever version is specified by this attribute, regardless of the version specified on the command line or by other higher targets that depend on this one. If you want to  Bug warning: This attribute sets the version for which Bazel builds your target,
          but due to #4815, the
          resulting stub script may still invoke the wrong interpreter version at runtime. See
          this
          workaround, which involves defining a  | 
| srcs_version | String; default is  srcsto be compatible with either Python
          2, Python 3, or both. To actually set the Python runtime version, use thepython_versionattribute of an
          executable Python rule (py_binaryorpy_test).Allowed values are:  Note that only the executable rules ( To get diagnostic information about which dependencies introduce version requirements,
          you can run the  
          bazel build <your target> \
              --aspects=@rules_python//python:defs.bzl%find_requirements \
              --output_groups=pyversioninfo
          -pyversioninfo.txtgiving information
          about why your target requires one Python version or another. Note that it works even if
          the given target failed to build due to a version conflict. | 
| stamp | Integer; default is  
 Stamped binaries are not rebuilt unless their dependencies change. | 
py_library
View rule sourcepy_library(name, deps, srcs, data, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, features, imports, licenses, restricted_to, srcs_version, tags, target_compatible_with, testonly, visibility)
Arguments
| Attributes | |
|---|---|
| name | Name; required A unique name for this target. | 
| deps | List of labels; default is  depsat
          
          Typical attributes defined by most build rules.
          These are generallypy_libraryrules. | 
| srcs | List of labels; default is  .py) files that are processed to create the target.
        This includes all your checked-in code and any generated source files. | 
| imports | List of strings; default is  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  
          Absolute paths (paths that start with  | 
| srcs_version | String; default is  srcsto be compatible with either Python
          2, Python 3, or both. To actually set the Python runtime version, use thepython_versionattribute of an
          executable Python rule (py_binaryorpy_test).Allowed values are:  Note that only the executable rules ( To get diagnostic information about which dependencies introduce version requirements,
          you can run the  
          bazel build <your target> \
              --aspects=@rules_python//python:defs.bzl%find_requirements \
              --output_groups=pyversioninfo
          -pyversioninfo.txtgiving information
          about why your target requires one Python version or another. Note that it works even if
          the given target failed to build due to a version conflict. | 
py_test
View rule sourcepy_test(name, deps, srcs, data, args, compatible_with, deprecation, distribs, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, imports, legacy_create_init, licenses, local, main, python_version, restricted_to, shard_count, size, srcs_version, stamp, tags, target_compatible_with, testonly, timeout, toolchains, visibility)
A py_test() rule compiles a test.  A test is a binary wrapper
 around some test code.
Examples
py_test(
    name = "runtest_test",
    srcs = ["runtest_test.py"],
    deps = [
        "//path/to/a/py/library",
    ],
)
It's also possible to specify a main module:
py_test(
    name = "runtest_test",
    srcs = [
        "runtest_main.py",
        "runtest_lib.py",
    ],
    main = "runtest_main.py",
)
Arguments
| Attributes | |
|---|---|
| name | Name; required A unique name for this target. | 
| deps | List of labels; default is  depsat
          
          Typical attributes defined by most build rules.
          These are generallypy_libraryrules. | 
| srcs | List of labels; requiredThe list of source ( .py) files that are processed to create the target.
          This includes all your checked-in code and any generated source files. Library targets
          belong indepsinstead, while other binary files needed at runtime belong indata. | 
| imports | List of strings; default is  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  
          Absolute paths (paths that start with  | 
| legacy_create_init | Integer; default is  --incompatible_default_to_explicit_init_pyis used. If false, the user is
          responsible for creating (possibly empty) __init__.py files and adding them to thesrcsof Python targets as required. | 
| main | Label; default is  srcs. If left unspecified,nameis used instead (see above). Ifnamedoes not
          match any filename insrcs,mainmust be specified. | 
| python_version | String; nonconfigurable; default is  deps) for Python 2 or Python
          3. Valid values are"PY2"and"PY3"(the default).The Python version is always reset (possibly by default) to whatever version is specified by this attribute, regardless of the version specified on the command line or by other higher targets that depend on this one. If you want to  Bug warning: This attribute sets the version for which Bazel builds your target,
          but due to #4815, the
          resulting stub script may still invoke the wrong interpreter version at runtime. See
          this
          workaround, which involves defining a  | 
| srcs_version | String; default is  srcsto be compatible with either Python
          2, Python 3, or both. To actually set the Python runtime version, use thepython_versionattribute of an
          executable Python rule (py_binaryorpy_test).Allowed values are:  Note that only the executable rules ( To get diagnostic information about which dependencies introduce version requirements,
          you can run the  
          bazel build <your target> \
              --aspects=@rules_python//python:defs.bzl%find_requirements \
              --output_groups=pyversioninfo
          -pyversioninfo.txtgiving information
          about why your target requires one Python version or another. Note that it works even if
          the given target failed to build due to a version conflict. | 
| stamp | Integer; default is  | 
py_runtime
View rule sourcepy_runtime(name, bootstrap_template, compatible_with, coverage_tool, deprecation, distribs, features, files, interpreter, interpreter_path, licenses, python_version, restricted_to, stub_shebang, tags, target_compatible_with, testonly, visibility)
Represents a Python runtime used to execute Python code.
A py_runtime target can represent 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 an executable target that acts as the interpreter. In
both cases, an "interpreter" means 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.
A platform runtime is by its nature non-hermetic. It imposes a requirement on the target platform to have an interpreter located at a specific path. An in-build runtime may or may not be hermetic, depending on whether it points to a checked-in interpreter or a wrapper script that accesses the system interpreter.
Example:
py_runtime(
    name = "python-2.7.12",
    files = glob(["python-2.7.12/**"]),
    interpreter = "python-2.7.12/bin/python",
)
py_runtime(
    name = "python-3.6.0",
    interpreter_path = "/opt/pyenv/versions/3.6.0/bin/python",
)
Arguments
| Attributes | |
|---|---|
| name | Name; required A unique name for this target. | 
| bootstrap_template | Label; default is  | 
| coverage_tool | Label; default is  py_binaryandpy_testtargets.If set, the target must either produce a single file or be and executable target. The path to the single file, or the executable if the target is executable, determines the entry point for the python coverage tool. The target and its runfiles will be added to the runfiles when coverage is enabled. The entry point for the tool must be loadable by a python interpreter (e.g. a
         | 
| files | List of labels; default is  | 
| interpreter | Label; default is  | 
| interpreter_path | String; default is  | 
| python_version | String; default is  "PY2"and"PY3".The default value is controlled by the  | 
| stub_shebang | String; default is  py_binarytargets.See issue 8685 for motivation. Does not apply to Windows. |