compile_pip_requirements

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>_test
  • update with bazel run <name>.update

PARAMETERS

NameDescriptionDefault Value
namebase name for generated targets, typically “requirements”none
extra_argspassed to pip-compile[]
visibilitypassed to both the _test and .update rules[“//visibility:private”]
requirements_infile expressing desired dependenciesNone
requirements_txtresult of “compiling” the requirements.in fileNone
tagstagging attribute common to all build rules, passed to both the _test and .update rulesNone
kwargsother bazel attributes passed to the “_test” rulenone

pip_import

Rule for installing packages listed in a requirements file.

PARAMETERS

NameDescriptionDefault Value
kwargs - none

pip_install

Accepts a requirements.txt file and installs the dependencies listed within.

Those dependencies become available in a generated requirements.bzl file.

This macro runs a repository rule that invokes pip. In your WORKSPACE file:

pip_install(
    requirements = ":requirements.txt",
)

You can then reference installed dependencies from a BUILD file with:

load("@pip//:requirements.bzl", "requirement")
py_library(
    name = "bar",
    ...
    deps = [
       "//my/other:dep",
       requirement("requests"),
       requirement("numpy"),
    ],
)

Note that this convenience comes with a cost. Analysis of any BUILD file which loads the requirements helper in this way will cause an eager-fetch of all the pip dependencies, even if no python targets are requested to be built. In a multi-language repo, this may cause developers to fetch dependencies they don't need, so consider using the long form for dependencies if this happens.

In addition to the requirement macro, which is used to access the py_library target generated from a package's wheel, the generated requirements.bzl file contains functionality for exposing entry points as py_binary targets.

load("@pip_deps//: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_deps//:requirements.bzl", "entry_point")

alias(
    name = "flake8",
    actual = entry_point("flake8"),
)

PARAMETERS

NameDescriptionDefault Value
requirementsA ‘requirements.txt’ pip requirements file.none
nameA unique name for the created external repository (default ‘pip’).“pip”
kwargsKeyword arguments passed directly to the pip_repository repository rule.none

pip_parse

Accepts a locked/compiled requirements file and installs the dependencies listed within.

Those dependencies become available in a generated requirements.bzl file.

This macro runs a repository rule that invokes pip. In your WORKSPACE file:

load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
    name = "pip_deps",
    requirements_lock = ":requirements.txt",
)

load("@pip_deps//:requirements.bzl", "install_deps")

install_deps()

You can then reference installed dependencies from a BUILD file with:

load("@pip_deps//:requirements.bzl", "requirement")

py_library(
    name = "bar",
    ...
    deps = [
       "//my/other:dep",
       requirement("requests"),
       requirement("numpy"),
    ],
)

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("@pip_deps//: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_deps//:requirements.bzl", "entry_point")

alias(
    name = "flake8",
    actual = entry_point("flake8"),
)

PARAMETERS

NameDescriptionDefault Value
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’.none
nameThe name of the generated repository.“pip_parsed_deps”
kwargsAdditional keyword arguments for the underlying pip_repository rule.none

pip_repositories

Obsolete macro to pull in dependencies needed to use the pip_import rule.

PARAMETERS