//python:pip.bzl¶
Rules for pip integration.
This contains a set of rules that are used to support inclusion of third-party
dependencies via fully locked requirements.txt files. Some of the exported
symbols should not be used and they are either undocumented here or marked as
for internal use only.
compile_pip_requirements¶
compile_pip_requirements(name, src=None, extra_args=[], extra_deps=[], generate_hashes=True, py_binary=<function py_binary from //python:py_binary.bzl>, py_test=<function py_test from //python:py_test.bzl>, requirements_in=None, requirements_txt=None, requirements_darwin=None, requirements_linux=None, requirements_windows=None, visibility=[“//visibility:private”], tags=None, kwargs)
Generates targets for managing pip dependencies with pip-compile.
By default this rules generates a filegroup named “[name]” which can be included in the data
of some other compile_pip_requirements rule that references these requirements
(e.g. with -r ../other/requirements.txt).
It also generates two targets for running pip-compile:
validate with
bazel test [name]_testupdate with
bazel run [name].update
If you are using a version control system, the requirements.txt generated by this rule should be checked into it to ensure that all developers/users have the same dependency versions.
PARAMETERS ¶
- name¶:
base name for generated targets, typically “requirements”.
- src¶:
(default
None) file containing inputs to dependency resolution. If not specified, defaults topyproject.toml. Supported formats are:a requirements text file, usually named
requirements.inA
.tomlfile, where theproject.dependencieslist is used as per PEP621.
- extra_args¶:
(default
[]) passed to pip-compile.- extra_deps¶:
(default
[]) extra dependencies passed to pip-compile.- generate_hashes¶:
(default
True) whether to put hashes in the requirements_txt file.- py_binary¶:
(default
<function py_binary from //python:py_binary.bzl>) the py_binary rule to be used.- py_test¶:
(default
<function py_test from //python:py_test.bzl>) the py_test rule to be used.- requirements_in¶:
(default
None) file expressing desired dependencies. Deprecated, use src instead.- requirements_txt¶:
(default
None) result of “compiling” the requirements.in file.- requirements_darwin¶:
(default
None) File of darwin specific resolve output to check validate if requirement.in has changes.- requirements_linux¶:
(default
None) File of linux specific resolve output to check validate if requirement.in has changes.- requirements_windows¶:
(default
None) File of windows specific resolve output to check validate if requirement.in has changes.- visibility¶:
(default
["//visibility:private"]) passed to both the _test and .update rules.- tags¶:
- kwargs¶:
other bazel attributes passed to the “_test” rule.
multi_pip_parse¶
multi_pip_parse(name, default_version, python_versions, python_interpreter_target, requirements_lock, kwargs)
NOT INTENDED FOR DIRECT USE!
This is intended to be used by the multi_pip_parse implementation in the template of the multi_toolchain_aliases repository rule.
PARAMETERS ¶
- name¶:
the name of the multi_pip_parse repository.
- default_version¶:
the default Python version.
- python_versions¶:
all Python toolchain versions currently registered.
- python_interpreter_target¶:
a dictionary which keys are Python versions and values are resolved host interpreters.
- requirements_lock¶:
a dictionary which keys are Python versions and values are locked requirements files.
- kwargs¶:
extra arguments passed to all wrapped pip_parse.
- RETURNS¶
The internal implementation of multi_pip_parse repository rule.
package_annotation¶
package_annotation(additive_build_content=None, copy_files={}, copy_executables={}, data=[], data_exclude_glob=[], srcs_exclude_glob=[])
Annotations to apply to the BUILD file content from package generated from a pip_repository rule.
PARAMETERS ¶
- additive_build_content¶:
(default
None) Raw text to add to the generatedBUILDfile of a package.- copy_files¶:
(default
{}) A mapping ofsrcandoutfiles for @bazel_skylib//rules:copy_file.bzl- copy_executables¶:
(default
{}) A mapping ofsrcandoutfiles for @bazel_skylib//rules:copy_file.bzl. Targets generated here will also be flagged as executable.- data¶:
(default
[]) A list of labels to add asdatadependencies to the generatedpy_librarytarget.- data_exclude_glob¶:
(default
[]) A list of exclude glob patterns to add asdatato the generatedpy_librarytarget.- srcs_exclude_glob¶:
(default
[]) A list of labels to add assrcsto the generatedpy_librarytarget.
- RETURNS¶
str: A json encoded string of the provided content.
pip_parse¶
pip_parse(name, repo_mapping, annotations={}, requirements_darwin=None, requirements_linux=None, requirements_lock=None, requirements_windows=None, download_only=False, enable_implicit_namespace_pkgs=False, environment={}, envsubst=[], experimental_requirement_cycles={}, experimental_target_platforms=[], extra_pip_args=[], isolated=True, pip_data_exclude=[], python_interpreter=””, python_interpreter_target=None, quiet=True, repo_prefix=””, timeout=600)
Accepts a locked/compiled requirements file and installs the dependencies listed within.
Those dependencies become available in a generated requirements.bzl file.
You can instead check this requirements.bzl file into your repo, see the “vendoring” section below.
In your WORKSPACE file:
load("@rules_python//python:pip.bzl", "pip_parse")
pip_parse(
name = "pypi",
requirements_lock = ":requirements.txt",
)
load("@pypi//:requirements.bzl", "install_deps")
install_deps()
You can then reference installed dependencies from a BUILD file with the alias targets generated in the same repo, for example, for PyYAML we would have the following:
@pypi//pyyamland@pypi//pyyaml:pkgboth point to thepy_librarycreated after extracting thePyYAMLpackage.@pypi//pyyaml:datapoints to the extra data included in the package.@pypi//pyyaml:dist_infopoints to thedist-infofiles in the package.@pypi//pyyaml:whlpoints to the wheel file that was extracted.
py_library(
name = "bar",
...
deps = [
"//my/other:dep",
"@pypi//numpy",
"@pypi//requests",
],
)
or
load("@pypi//:requirements.bzl", "requirement")
py_library(
name = "bar",
...
deps = [
"//my/other:dep",
requirement("numpy"),
requirement("requests"),
],
)
In addition to the requirement macro, which is used to access the generated py_library
target generated from a package’s wheel, The generated requirements.bzl file contains
functionality for exposing entry points as py_binary targets as well.
load("@pypi//:requirements.bzl", "entry_point")
alias(
name = "pip-compile",
actual = entry_point(
pkg = "pip-tools",
script = "pip-compile",
),
)
Note that for packages whose name and script are the same, only the name of the package
is needed when calling the entry_point macro.
load("@pip//:requirements.bzl", "entry_point")
alias(
name = "flake8",
actual = entry_point("flake8"),
)
Vendoring the requirements.bzl file¶
In some cases you may not want to generate the requirements.bzl file as a repository rule while Bazel is fetching dependencies. For example, if you produce a reusable Bazel module such as a ruleset, you may want to include the requirements.bzl file rather than make your users install the WORKSPACE setup to generate it. See https://github.com/bazelbuild/rules_python/issues/608
This is the same workflow as Gazelle, which creates go_repository rules with
update-repos
To do this, use the “write to source file” pattern documented in https://blog.aspect.dev/bazel-can-write-to-the-source-folder to put a copy of the generated requirements.bzl into your project. Then load the requirements.bzl file directly rather than from the generated repository. See the example in rules_python/examples/pip_parse_vendored.
ATTRIBUTES ¶
- name¶:
(required Name) A unique name for this repository.
- repo_mapping¶:
(optional dict of string to string) In
WORKSPACEcontext only: 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).This attribute is not supported in
MODULE.bazelcontext (when invoking a repository rule inside a module extension’s implementation function).- annotations¶:
(optional dict of string to string, default
{}) Optional annotations to apply to packages- 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’. Note that if your lockfile is platform-dependent, you can use therequirements_[platform]attributes.- requirements_windows¶:
(optional label, default
None) Override the requirements_lock attribute when the host platform is Windows- download_only¶:
(optional [bool][bool], 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 [bool][bool], 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 eitherlegacy_create_init=Falseor the global Bazel option--incompatible_default_to_explicit_init_pyto prevent__init__.pybeing automatically generated in every directory.This option is required to support some packages which cannot handle the conversion to pkg-util style.
- environment¶:
(optional dict of string to string, default
{}) Environment variables to set in the pip subprocess. Can be used to set common variables such ashttp_proxy,https_proxyandno_proxyNote that pip is run with “–isolated” on the CLI soPIP_<VAR>_<NAME>style env vars are ignored, but env vars that control requests and urllib3 can be passed. If you needPIP_<VAR>_<NAME>, take a look atextra_pip_argsandenvsubst.- envsubst¶:
(optional list of string, default
[]) A list of environment variables to substitute (e.g.["PIP_INDEX_URL", "PIP_RETRIES"]). The corresponding variables are expanded inextra_pip_argsusing the syntax$VARNAMEor${VARNAME}(expanding to empty string if unset) or${VARNAME:-default}(expanding to default if the variable is unset or empty in the environment). Note: On Bazel 6 and Bazel 7 changes to the variables named here do not cause packages to be re-fetched. Don’t fetch different things based on the value of these variables.- experimental_requirement_cycles¶:
(optional dict of string to list of string, default
{}) A mapping of dependency cycle names to a list of requirements which form that cycle.Requirements which form cycles will be installed together and taken as dependencies together in order to ensure that the cycle is always satisified.
Example:
sphinxdepends onsphinxcontrib-serializinghtmlWhen listing both as requirements, alapy_binary( name = "doctool", ... deps = [ "@pypi//sphinx:pkg", "@pypi//sphinxcontrib_serializinghtml", ] )
Will produce a Bazel error such as
ERROR: .../external/pypi_sphinxcontrib_serializinghtml/BUILD.bazel:44:6: in alias rule @pypi_sphinxcontrib_serializinghtml//:pkg: cycle in dependency graph: //:doctool (...) @pypi//sphinxcontrib_serializinghtml:pkg (...) .-> @pypi_sphinxcontrib_serializinghtml//:pkg (...) | @pypi_sphinxcontrib_serializinghtml//:_pkg (...) | @pypi_sphinx//:pkg (...) | @pypi_sphinx//:_pkg (...) `-- @pypi_sphinxcontrib_serializinghtml//:pkg (...)Which we can resolve by configuring these two requirements to be installed together as a cycle
pip_parse( ... experimental_requirement_cycles = { "sphinx": [ "sphinx", "sphinxcontrib-serializinghtml", ] }, )
Warning: If a dependency participates in multiple cycles, all of those cycles must be collapsed down to one. For instance
a <-> banda <-> ccannot be listed as two separate cycles.- experimental_target_platforms¶:
(optional list of string, default
[]) A list of platforms that we will generate the conditional dependency graph for cross platform wheels by parsing the wheel metadata. This will generate the correct dependencies for packages likesphinxorpylint, which includecoloramawhen installed and used on Windows platforms.An empty list means falling back to the legacy behaviour where the host platform is the target platform.
WARNING: It may not work as expected in cases where the python interpreter implementation that is being used at runtime is different between different platforms. This has been tested for CPython only.
For specific target platforms use values of the form
<os>_<arch>where<os>is one oflinux,osx,windowsand arch is one ofx86_64,x86_32,aarch64,s390xandppc64le.You can also target a specific Python version by using
cp3<minor_version>_<os>_<arch>. If multiple python versions are specified as target platforms, then select statements of thelibandwhltargets will include usage of version aware toolchain config settings like@rules_python//python/config_settings:is_python_3.y.Special values:
host(for generating deps for the host platform only) and<prefix>_*values. For example,cp39_*,linux_*,cp39_linux_*.NOTE: this is not for cross-compiling Python wheels but rather for parsing the
whlMETADATA correctly.- extra_pip_args¶:
(optional list of string, default
[]) Extra arguments to pass on to pip. Must not contain spaces.Supports environment variables using the syntax
$VARNAMEor${VARNAME}(expanding to empty string if unset) or${VARNAME:-default}(expanding to default if the variable is unset or empty in the environment), if"VARNAME"is listed in theenvsubstattribute. See alsoenvsubst.- isolated¶:
(optional [bool][bool], default
True) Whether or not to pass the –isolated flag to the underlying pip command. Alternatively, theRULES_PYTHON_PIP_ISOLATEDenvironment variable can be used to control this flag.- pip_data_exclude¶:
(optional list of string, 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’sPATHenvironment variable. If no value is setpython3is defaulted for Unix systems andpython.exefor 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. An example value: “@python3_x86_64-unknown-linux-gnu//:python”.- quiet¶:
(optional [bool][bool], default
True) If True, suppress printing stdout and stderr output to the terminal.- repo_prefix¶:
(optional string, default
"") Prefix for the generated packages will be of the form@<prefix><sanitized-package-name>//...- timeout¶:
(optional [int][int], default
600) Timeout (in seconds) on the rule’s execution duration.
ENVIRONMENT VARIABLES ¶
RULES_PYTHON_PIP_ISOLATEDRULES_PYTHON_REPO_DEBUG
pip_utils.normalize_name¶
pip_utils.normalize_name(name)
normalize a PyPI package name and return a valid bazel label.
PARAMETERS ¶
- name¶:
str, the PyPI package name.
- RETURNS¶
a normalized name as a string.
whl_library_alias¶
whl_library_alias(name, repo_mapping, default_version=””, version_map, wheel_name)
ATTRIBUTES ¶
- name¶:
(required Name) A unique name for this repository.
- repo_mapping¶:
(optional dict of string to string) In
WORKSPACEcontext only: 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).This attribute is not supported in
MODULE.bazelcontext (when invoking a repository rule inside a module extension’s implementation function).- default_version¶:
(optional string, default
"") Optional Python version in major.minor format, e.g. ‘3.10’.The Python version of the wheel to use when the versions fromversion_mapdon’t match. This allows the default (version unaware) rules to match and select a wheel. If not specified, then the default rules won’t be able to resolve a wheel and an error will occur.- version_map¶:
- wheel_name¶:
(required string)