Python Rules
Rules
py_binary
py_binary(name, deps, srcs, data, args, compatible_with, default_python_version, deprecation, distribs, features, imports, legacy_create_init, licenses, main, output_licenses, python_version, restricted_to, srcs_version, stamp, tags, 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 = [ "//pyglib", ":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 = [":testlib"], ) java_library( name = "testing", srcs = glob(["*.java"]), data = [":test_main"] )
Arguments
Attributes | |
---|---|
name |
A unique name for this target. If main is 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 called
main.py , then your name should be main .
|
deps
|
deps at
Attributes common to all build rules.
These can be
py_binary rules,
py_library rules.
|
srcs
|
srcs and
deps is loose. The .py files
probably belong in srcs and library targets probably belong
in deps , but don't worry about it too much.
|
data
|
data at
Attributes common to all build rules.
Also see the data argument of
the py_library rule for details.
|
default_python_version
|
deps .
Valid values are "PY2" (default) or "PY3" .
Python 3 support is experimental.
If both this attribute and |
imports
|
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
|
|
main
|
srcs . If left unspecified,
name is used instead (see above). If name does not
match any filename in srcs , main must be specified.
|
python_version
|
default_python_version . Only available when
--experimental_better_python_version_mixing is enabled. If both this and
default_python_version are supplied, the latter will be ignored.
|
srcs_version
|
.py source
files listed in the srcs of this rule are compatible with.
Please reference to py_runtime rules for
determining the python version.
Valid values are:"PY2ONLY" -
Python 2 code that is not suitable for 2to3 conversion."PY2" -
Python 2 code that is expected to work when run through 2to3 ."PY2AND3" -
Code that is compatible with both Python 2 and 3 without
2to3 conversion."PY3ONLY" -
Python 3 code that will not run on Python 2."PY3" -
A synonym for PY3ONLY. |
stamp
|
|
toolchains
|
|
py_library
py_library(name, deps, srcs, data, compatible_with, deprecation, distribs, features, imports, licenses, restricted_to, srcs_version, tags, testonly, visibility)
Arguments
Attributes | |
---|---|
name |
A unique name for this target. |
deps
|
deps at
Attributes common to all build rules.
In practice, these arguments are treated like those in srcs ;
you may move items between these lists willy-nilly. It's probably more
readable to keep your .py files in your srcs .
|
srcs
|
|
data
|
data at
Attributes common to all build rules.
|
imports
|
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
|
.py source
files listed in the srcs of this rule are compatible with.
Please reference to py_runtime rules for
determining the python version.
Valid values are:"PY2ONLY" -
Python 2 code that is not suitable for 2to3 conversion."PY2" -
Python 2 code that is expected to work when run through 2to3 ."PY2AND3" -
Code that is compatible with both Python 2 and 3 without
2to3 conversion."PY3ONLY" -
Python 3 code that will not run on Python 2."PY3" -
A synonym for PY3ONLY. |
py_test
py_test(name, deps, srcs, data, args, compatible_with, default_python_version, deprecation, distribs, exec_compatible_with, features, flaky, imports, legacy_create_init, licenses, local, main, python_version, restricted_to, shard_count, size, srcs_version, stamp, tags, 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 |
A unique name for this target. |
deps
|
deps at
Attributes common to all build rules.
These can be
py_binary rules,
py_library rules.
|
srcs
|
srcs and
deps is loose. The .py files
probably belong in srcs and library targets probably belong
in deps , but don't worry about it too much.
|
data
|
data at
Attributes common to all build rules.
Also see the data argument of
the py_library rule for details.
|
default_python_version
|
deps .
Valid values are "PY2" (default) or "PY3" .
Python 3 support is experimental.
If both this attribute and |
imports
|
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
|
|
main
|
srcs . If left unspecified,
name is used instead (see above). If name does not
match any filename in srcs , main must be specified.
|
python_version
|
default_python_version . Only available when
--experimental_better_python_version_mixing is enabled. If both this and
default_python_version are supplied, the latter will be ignored.
|
srcs_version
|
.py source
files listed in the srcs of this rule are compatible with.
Please reference to py_runtime rules for
determining the python version.
Valid values are:"PY2ONLY" -
Python 2 code that is not suitable for 2to3 conversion."PY2" -
Python 2 code that is expected to work when run through 2to3 ."PY2AND3" -
Code that is compatible with both Python 2 and 3 without
2to3 conversion."PY3ONLY" -
Python 3 code that will not run on Python 2."PY3" -
A synonym for PY3ONLY. |
stamp
|
|
toolchains
|
|
py_runtime
py_runtime(name, compatible_with, deprecation, distribs, features, files, interpreter, interpreter_path, licenses, restricted_to, tags, testonly, visibility)
Specifies the configuration for a Python runtime. This rule can either describe a Python runtime in the source tree or one at a well-known absolute path.
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", files = [], interpreter_path = "/opt/pyenv/versions/3.6.0/bin/python", )
Arguments
Attributes | |
---|---|
name |
A unique name for this target. |
files
|
|
interpreter
|
|
interpreter_path
|
|