Bazel build file load multiple dependencies

2k Views Asked by At

I have a Python repo that contains two requirement files. In my WORKSPACE file, I imported both the dependencies.

requirements-1.txt:

numpy

requirements-2.txt:

scipy

WORKSPACE:

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

pip_install(
    name = "deps_1",
    requirements = "//:requirements-1.txt",
)

pip_install(
    name = "deps_2",
    requirements = "//folder2:requirements-2.txt",
)

I have a BUILD file like this:

load("@rules_python//python:defs.bzl", "py_library")
load("@deps_1//:requirements.bzl", "requirement")
load("@deps_2//:requirements.bzl", "requirement")


py_library(
    name = "test",
    srcs = ["test.py"],
    deps = [
        requirement("numpy"),
        requirement("scipy"),
    ],
)

The build can't import numpy

no such package '@deps//pypi__numpy': BUILD file not found in directory 'pypi__numpy' of external repository @deps2. Add a BUILD file to a directory to mark it as a package.

How do I load two dependencies in the same BUILD file?

1

There are 1 best solutions below

0
On BEST ANSWER

In general, you can do this by loading the two requirements with different names. Like this:

load("@deps_1//:requirements.bzl", deps_1_requirement = "requirement")
load("@deps_2//:requirements.bzl", deps_2_requirement = "requirement")

Then you can use deps_1_requirement("numpy") and deps_2_requirement("scipy").

However, with those two particular libraries, scipy depends on numpy. If you load both of them, you'll end up with two different numpy copies. If they're both the same version, it'll probably work. If they're different versions, you might run into weird issues. I think you can just drop numpy from the list and your code should be able to import the single version that pip/rules_python bring in because it's a dependency of scipy.