Use in BUILD.bazel files

Once you have set up the dependencies, you are ready to start using them in yourBUILD.bazelfiles. If you haven’t done so yet, set it up by following these docs:

  1. WORKSPACE

  2. bzlmod

To refer to targets in a hub repopypi, you can do one of two things:

py_library(name="my_lib",deps=["@pypi//numpy",],)

Or use therequirement helper that needs to be loaded from thehub repo itself:

load("@pypi//:requirements.bzl","requirement")py_library(deps=[requirement("numpy")],)

Note that the usage of therequirement helper is not advised and can be problematic. See thenotes below.

Note that the hub repo contains the following targets for each package:

  • @pypi//numpy - shorthand for@pypi//numpy:numpy. This is analias to@pypi//numpy:pkg.

  • @pypi//numpy:pkg - thepy_library target automatically generated by the repositoryrules.

  • @pypi//numpy:data - thefilegroup for all of the extra files that are includedas data in thepkg target.

  • @pypi//numpy:dist_info - thefilegroup for all of the files in the<pkgprefixwithversion>.distinfo directory.

  • @pypi//numpy:extracted_whl_files - afilegroup of all the filesextracted from the whl file.

  • @pypi//numpy:whl - thefilegroup that is the.whl file itself, which includes alltransitive dependencies via thefilegroup.data attribute.

Added in version 1.6.0:The:extracted_whl_files target was added

Entry points

If you would like to accessentry points, see thepy_console_script_binary rule documentation,which can help you create apy_binary target for a particular console script exposed by a package.

‘Extras’ dependencies

Any “extras” specified in the requirements lock file will be automatically addedas transitive dependencies of the package. In the example above, you’d just putrequirement("useful_dep") or@pypi//useful_dep.

Consuming Wheel Dists Directly

If you need to depend on the wheel dists themselves (for instance, to pass themto some other packaging tool), you can get a handle to them with thewhl_requirement macro. For example:

load("@pypi//:requirements.bzl","whl_requirement")filegroup(name="whl_files",data=[# This is equivalent to "@pypi//boto3:whl"whl_requirement("boto3"),])

Creating a filegroup of files within a whl

The rulewhl_filegroup exists as an easy way to extract the necessary filesfrom a whl file without needing to modify theBUILD.bazel contents of thewhl repositories generated viapip_repository. Use it similarly to thefilegroupabove. See the API docs for more information.

A note about using the requirement helper

Each extracted wheel repo contains apy_library target representingthe wheel’s contents. There are two ways to access this library. Thefirst uses therequirement() function defined in the centralrepo’s//:requirements.bzl file. This function maps a pip packagename to a label:

load("@my_deps//:requirements.bzl","requirement")py_library(name="mylib",srcs=["mylib.py"],deps=[":myotherlib",requirement("some_pip_dep"),requirement("another_pip_dep"),])

The reasonrequirement() exists is to insulate users fromchanges to the underlying repository and label strings. However, thoselabels have become directly used, so they aren’t able to easily change regardless.

On the other hand, using therequirement() helper has several drawbacks:

  • It doesn’t work withbuildifier.

  • It doesn’t work withbuildozer.

  • It adds an extra layer on top of normal mechanisms to refer to targets.

  • It does not scale well, as each type of target needs a new macro to be loaded and imported.

If you don’t want to userequirement(), you can use the library labels directly instead. Forpip_parse, the labels are of the following form:

@{name}//{package}

Herename is thename attribute that was passed topip_parse andpackage is the pip package name with characters that are illegal inBazel label names (e.g.-,.) replaced with_. If you need toupdatename from “old” to “new”, then you can run the followingbuildozer command:

buildozer'substitute deps @old//([^/]+) @new//${1}'//...:*