Rules
pip_repository
A rule for importing requirements.txt
dependencies into Bazel.
This rule imports a requirements.txt
file and generates a new
requirements.bzl
file. This is used via the WORKSPACE
pattern:
pip_repository(
name = "foo",
requirements = ":requirements.txt",
)
You can then reference imported dependencies from your BUILD
file with:
load("@foo//:requirements.bzl", "requirement")
py_library(
name = "bar",
...
deps = [
"//my/other:dep",
requirement("requests"),
requirement("numpy"),
],
)
Or alternatively:
load("@foo//:requirements.bzl", "all_requirements")
py_binary(
name = "baz",
...
deps = [
":foo",
] + all_requirements,
)
Example usage (generated):
load("@rules_python//python/pip_install:pip_repository.bzl", "pip_repository")
pip_repository(
# A unique name for this repository.
name = "",
# A dictionary from local repository name to global repository name
repo_mapping = {},
)
name
Required name.
A unique name for this repository.
annotations
Optional dictionary: String → String.
Default: {}
Optional annotations to apply to packages
bzlmod
Optional boolean.
Default: False
Whether this repository rule is invoked under bzlmod, in which case we do not create the install_deps() macro.
download_only
Optional boolean.
Default: False
Whether to use "pip download" instead of "pip wheel". Disables building wheels from source, but allows use of --platform, --python-version, --implementation, and --abi in --extra_pip_args to download wheels for a different platform from the host platform.
enable_implicit_namespace_pkgs
Optional boolean.
Default: False
If true, disables conversion of native namespace packages into pkg-util style namespace packages. When set all py_binary
and py_test targets must specify either legacy_create_init=False
or the global Bazel option
--incompatible_default_to_explicit_init_py
to prevent __init__.py
being automatically generated in every directory.
This option is required to support some packages which cannot handle the conversion to pkg-util style.
environment
Optional dictionary: String → String.
Default: {}
Environment variables to set in the pip subprocess.
Can be used to set common variables such as http_proxy
, https_proxy
and no_proxy
Note that pip is run with "--isolated" on the CLI so PIP_<VAR>_<NAME>
style env vars are ignored, but env vars that control requests and urllib3
can be passed.
extra_pip_args
Optional list of strings.
Default: []
Extra arguments to pass on to pip. Must not contain spaces.
isolated
Optional boolean.
Default: True
Whether or not to pass the --isolated flag to
the underlying pip command. Alternatively, the RULES_PYTHON_PIP_ISOLATED
enviornment varaible can be used
to control this flag.
pip_data_exclude
Optional list of strings.
Default: []
Additional data exclusion parameters to add to the pip packages BUILD file.
python_interpreter
Optional string.
Default: ""
The python interpreter to use. This can either be an absolute path or the name
of a binary found on the host's PATH
environment variable. If no value is set
python3
is defaulted for Unix systems and python.exe
for Windows.
python_interpreter_target
Optional label.
Default: None
If you are using a custom python interpreter built by another repository rule, use this attribute to specify its BUILD target. This allows pip_repository to invoke pip using the same interpreter as your toolchain. If set, takes precedence over python_interpreter.
quiet
Optional boolean.
Default: True
If True, suppress printing stdout and stderr output to the terminal.
repo_mapping
Required dictionary: String → String.
A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.
For example, an entry "@foo": "@bar"
declares that, for any time this repository depends on @foo
(such as a dependency on @foo//some:target
, it should actually resolve that dependency within globally-declared @bar
(@bar//some:target
).
repo_prefix
Optional string.
Default: ""
Prefix for the generated packages will be of the form @<prefix><sanitized-package-name>//...
requirements_darwin
Optional label.
Default: None
Override the requirements_lock attribute when the host platform is Mac OS
requirements_linux
Optional label.
Default: None
Override the requirements_lock attribute when the host platform is Linux
requirements_lock
Optional label.
Default: None
A fully resolved 'requirements.txt' pip requirement file containing the transitive set of your dependencies. If this file is passed instead of 'requirements' no resolve will take place and pip_repository will create individual repositories for each of your dependencies so that wheels are fetched/built only for the targets specified by 'build/run/test'.
requirements_windows
Optional label.
Default: None
Override the requirements_lock attribute when the host platform is Windows
timeout
Optional integer.
Default: 600
Timeout (in seconds) on the rule's execution duration.
whl_library
Download and extracts a single wheel based into a bazel repo based on the requirement string passed in. Instantiated from pip_repository and inherits config options from there.
Example usage (generated):
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_library")
whl_library(
# A unique name for this repository.
name = "",
# Pointer to parent repo name
repo = "",
# A dictionary from local repository name to global repository name
repo_mapping = {},
# Python requirement string describing the package to make available
requirement = "",
)
name
Required name.
A unique name for this repository.
annotation
Optional label.
Default: None
Optional json encoded file containing annotation to apply to the extracted wheel. See package_annotation
download_only
Optional boolean.
Default: False
Whether to use "pip download" instead of "pip wheel". Disables building wheels from source, but allows use of --platform, --python-version, --implementation, and --abi in --extra_pip_args to download wheels for a different platform from the host platform.
enable_implicit_namespace_pkgs
Optional boolean.
Default: False
If true, disables conversion of native namespace packages into pkg-util style namespace packages. When set all py_binary
and py_test targets must specify either legacy_create_init=False
or the global Bazel option
--incompatible_default_to_explicit_init_py
to prevent __init__.py
being automatically generated in every directory.
This option is required to support some packages which cannot handle the conversion to pkg-util style.
environment
Optional dictionary: String → String.
Default: {}
Environment variables to set in the pip subprocess.
Can be used to set common variables such as http_proxy
, https_proxy
and no_proxy
Note that pip is run with "--isolated" on the CLI so PIP_<VAR>_<NAME>
style env vars are ignored, but env vars that control requests and urllib3
can be passed.
extra_pip_args
Optional list of strings.
Default: []
Extra arguments to pass on to pip. Must not contain spaces.
isolated
Optional boolean.
Default: True
Whether or not to pass the --isolated flag to
the underlying pip command. Alternatively, the RULES_PYTHON_PIP_ISOLATED
enviornment varaible can be used
to control this flag.
pip_data_exclude
Optional list of strings.
Default: []
Additional data exclusion parameters to add to the pip packages BUILD file.
python_interpreter
Optional string.
Default: ""
The python interpreter to use. This can either be an absolute path or the name
of a binary found on the host's PATH
environment variable. If no value is set
python3
is defaulted for Unix systems and python.exe
for Windows.
python_interpreter_target
Optional label.
Default: None
If you are using a custom python interpreter built by another repository rule, use this attribute to specify its BUILD target. This allows pip_repository to invoke pip using the same interpreter as your toolchain. If set, takes precedence over python_interpreter.
quiet
Optional boolean.
Default: True
If True, suppress printing stdout and stderr output to the terminal.
repo
Required string.
Pointer to parent repo name. Used to make these rules rerun if the parent repo changes.
repo_mapping
Required dictionary: String → String.
A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.
For example, an entry "@foo": "@bar"
declares that, for any time this repository depends on @foo
(such as a dependency on @foo//some:target
, it should actually resolve that dependency within globally-declared @bar
(@bar//some:target
).
repo_prefix
Optional string.
Default: ""
Prefix for the generated packages will be of the form @<prefix><sanitized-package-name>//...
requirement
Required string.
Python requirement string describing the package to make available
timeout
Optional integer.
Default: 600
Timeout (in seconds) on the rule's execution duration.
Macros and Functions
locked_requirements_label
Get the preferred label for a locked requirements file based on platform.
Example usage (generated):
load("@rules_python//python/pip_install:pip_repository.bzl", "locked_requirements_label")
locked_requirements_label(
# repository or module context
ctx = None,
# attributes for the repo rule or tag extension
attr = None,
)
ctx
Required.
repository or module context
attr
Required.
attributes for the repo rule or tag extension
package_annotation
Annotations to apply to the BUILD file content from package generated from a pip_repository
rule.
Example usage (generated):
load("@rules_python//python/pip_install:pip_repository.bzl", "package_annotation")
package_annotation(
)
additive_build_content
Optional. Default: None
Raw text to add to the generated BUILD
file of a package.
copy_files
Optional. Default: {}
A mapping of src
and out
files for @bazel_skylib//rules:copy_file.bzl
copy_executables
Optional. Default: {}
A mapping of src
and out
files for
@bazel_skylib//rules:copy_file.bzl. Targets generated here will also be flagged as
executable.
data
Optional. Default: []
A list of labels to add as data
dependencies to the generated py_library
target.
data_exclude_glob
Optional. Default: []
A list of exclude glob patterns to add as data
to the generated
py_library
target.
srcs_exclude_glob
Optional. Default: []
A list of labels to add as srcs
to the generated py_library
target.
use_isolated
Determine whether or not to pass the pip --isolated
flag to the pip invocation.
Example usage (generated):
load("@rules_python//python/pip_install:pip_repository.bzl", "use_isolated")
use_isolated(
# repository or module context
ctx = None,
# attributes for the repo rule or tag extension
attr = None,
)
ctx
Required.
repository or module context
attr
Required.
attributes for the repo rule or tag extension