pip_repository


pip_repository(<a href="#pip_repository-name">name</a>, <a href="#pip_repository-annotations">annotations</a>, <a href="#pip_repository-download_only">download_only</a>, <a href="#pip_repository-enable_implicit_namespace_pkgs">enable_implicit_namespace_pkgs</a>, <a href="#pip_repository-environment">environment</a>, <a href="#pip_repository-extra_pip_args">extra_pip_args</a>, <a href="#pip_repository-incompatible_generate_aliases">incompatible_generate_aliases</a>, <a href="#pip_repository-isolated">isolated</a>, <a href="#pip_repository-pip_data_exclude">pip_data_exclude</a>, <a href="#pip_repository-python_interpreter">python_interpreter</a>, <a href="#pip_repository-python_interpreter_target">python_interpreter_target</a>, <a href="#pip_repository-quiet">quiet</a>, <a href="#pip_repository-repo_mapping">repo_mapping</a>, <a href="#pip_repository-repo_prefix">repo_prefix</a>, <a href="#pip_repository-requirements_darwin">requirements_darwin</a>, <a href="#pip_repository-requirements_linux">requirements_linux</a>, <a href="#pip_repository-requirements_lock">requirements_lock</a>, <a href="#pip_repository-requirements_windows">requirements_windows</a>, <a href="#pip_repository-timeout">timeout</a>)

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,
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
annotationsOptional annotations to apply to packagesDictionary: String -> Stringoptional{}
download_onlyWhether 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.BooleanoptionalFalse
enable_implicit_namespace_pkgsIf 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.
BooleanoptionalFalse
environmentEnvironment 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.Dictionary: String -> Stringoptional{}
extra_pip_argsExtra arguments to pass on to pip. Must not contain spaces.List of stringsoptional[]
incompatible_generate_aliasesAllow generating aliases ‘@pip//<pkg>’ -> ‘@pip_<pkg>//:pkg’.BooleanoptionalFalse
isolatedWhether or not to pass the --isolated flag to the underlying pip command. Alternatively, the RULES_PYTHON_PIP_ISOLATED environment variable can be used to control this flag.BooleanoptionalTrue
pip_data_excludeAdditional data exclusion parameters to add to the pip packages BUILD file.List of stringsoptional[]
python_interpreterThe 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.Stringoptional""
python_interpreter_targetIf 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”.LabeloptionalNone
quietIf True, suppress printing stdout and stderr output to the terminal.BooleanoptionalTrue
repo_mappingA dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>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).Dictionary: String -> Stringrequired
repo_prefixPrefix for the generated packages will be of the form @<prefix><sanitized-package-name>//...Stringoptional""
requirements_darwinOverride the requirements_lock attribute when the host platform is Mac OSLabeloptionalNone
requirements_linuxOverride the requirements_lock attribute when the host platform is LinuxLabeloptionalNone
requirements_lockA 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’.LabeloptionalNone
requirements_windowsOverride the requirements_lock attribute when the host platform is WindowsLabeloptionalNone
timeoutTimeout (in seconds) on the rule's execution duration.Integeroptional600

pip_repository_bzlmod


pip_repository_bzlmod(<a href="#pip_repository_bzlmod-name">name</a>, <a href="#pip_repository_bzlmod-incompatible_generate_aliases">incompatible_generate_aliases</a>, <a href="#pip_repository_bzlmod-repo_mapping">repo_mapping</a>, <a href="#pip_repository_bzlmod-repo_name">repo_name</a>, <a href="#pip_repository_bzlmod-requirements_darwin">requirements_darwin</a>, <a href="#pip_repository_bzlmod-requirements_linux">requirements_linux</a>, <a href="#pip_repository_bzlmod-requirements_lock">requirements_lock</a>, <a href="#pip_repository_bzlmod-requirements_windows">requirements_windows</a>)

A rule for bzlmod pip_repository creation. Intended for private use only.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
incompatible_generate_aliasesAllow generating aliases in ‘@pip//:<pkg>’ -> ‘@pip_<pkg>//:pkg’. This replaces the aliases generated by the bzlmod tooling.BooleanoptionalFalse
repo_mappingA dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>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).Dictionary: String -> Stringrequired
repo_nameThe apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical nameStringrequired
requirements_darwinOverride the requirements_lock attribute when the host platform is Mac OSLabeloptionalNone
requirements_linuxOverride the requirements_lock attribute when the host platform is LinuxLabeloptionalNone
requirements_lockA 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’.LabeloptionalNone
requirements_windowsOverride the requirements_lock attribute when the host platform is WindowsLabeloptionalNone

whl_library


whl_library(<a href="#whl_library-name">name</a>, <a href="#whl_library-annotation">annotation</a>, <a href="#whl_library-download_only">download_only</a>, <a href="#whl_library-enable_implicit_namespace_pkgs">enable_implicit_namespace_pkgs</a>, <a href="#whl_library-environment">environment</a>, <a href="#whl_library-extra_pip_args">extra_pip_args</a>, <a href="#whl_library-isolated">isolated</a>, <a href="#whl_library-pip_data_exclude">pip_data_exclude</a>, <a href="#whl_library-python_interpreter">python_interpreter</a>, <a href="#whl_library-python_interpreter_target">python_interpreter_target</a>, <a href="#whl_library-quiet">quiet</a>, <a href="#whl_library-repo">repo</a>, <a href="#whl_library-repo_mapping">repo_mapping</a>, <a href="#whl_library-repo_prefix">repo_prefix</a>, <a href="#whl_library-requirement">requirement</a>, <a href="#whl_library-timeout">timeout</a>)

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.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
annotationOptional json encoded file containing annotation to apply to the extracted wheel. See package_annotationLabeloptionalNone
download_onlyWhether 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.BooleanoptionalFalse
enable_implicit_namespace_pkgsIf 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.
BooleanoptionalFalse
environmentEnvironment 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.Dictionary: String -> Stringoptional{}
extra_pip_argsExtra arguments to pass on to pip. Must not contain spaces.List of stringsoptional[]
isolatedWhether or not to pass the --isolated flag to the underlying pip command. Alternatively, the RULES_PYTHON_PIP_ISOLATED environment variable can be used to control this flag.BooleanoptionalTrue
pip_data_excludeAdditional data exclusion parameters to add to the pip packages BUILD file.List of stringsoptional[]
python_interpreterThe 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.Stringoptional""
python_interpreter_targetIf 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”.LabeloptionalNone
quietIf True, suppress printing stdout and stderr output to the terminal.BooleanoptionalTrue
repoPointer to parent repo name. Used to make these rules rerun if the parent repo changes.Stringrequired
repo_mappingA dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>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).Dictionary: String -> Stringrequired
repo_prefixPrefix for the generated packages will be of the form @<prefix><sanitized-package-name>//...Stringoptional""
requirementPython requirement string describing the package to make availableStringrequired
timeoutTimeout (in seconds) on the rule's execution duration.Integeroptional600

locked_requirements_label


locked_requirements_label(<a href="#locked_requirements_label-ctx">ctx</a>, <a href="#locked_requirements_label-attr">attr</a>)

Get the preferred label for a locked requirements file based on platform.

PARAMETERS

NameDescriptionDefault Value
ctxrepository or module contextnone
attrattributes for the repo rule or tag extensionnone

RETURNS

Label

package_annotation


package_annotation(<a href="#package_annotation-additive_build_content">additive_build_content</a>, <a href="#package_annotation-copy_files">copy_files</a>, <a href="#package_annotation-copy_executables">copy_executables</a>, <a href="#package_annotation-data">data</a>, <a href="#package_annotation-data_exclude_glob">data_exclude_glob</a>, <a href="#package_annotation-srcs_exclude_glob">srcs_exclude_glob</a>)

Annotations to apply to the BUILD file content from package generated from a pip_repository rule.

PARAMETERS

NameDescriptionDefault Value
additive_build_contentRaw text to add to the generated BUILD file of a package.None
copy_filesA mapping of src and out files for @bazel_skylib//rules:copy_file.bzl{}
copy_executablesA mapping of src and out files for @bazel_skylib//rules:copy_file.bzl. Targets generated here will also be flagged as executable.{}
dataA list of labels to add as data dependencies to the generated py_library target.[]
data_exclude_globA list of exclude glob patterns to add as data to the generated py_library target.[]
srcs_exclude_globA list of labels to add as srcs to the generated py_library target.[]

RETURNS

str: A json encoded string of the provided content.

use_isolated


use_isolated(<a href="#use_isolated-ctx">ctx</a>, <a href="#use_isolated-attr">attr</a>)

Determine whether or not to pass the pip --isolated flag to the pip invocation.

PARAMETERS

NameDescriptionDefault Value
ctxrepository or module contextnone
attrattributes for the repo rule or tag extensionnone

RETURNS

True if --isolated should be passed