Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bazel error: no such package: The repository could not be resolved: Repository is not defined and referenced by

I am trying to run a simple python binary with bazel using bazel run projects/my-python-app/.... But when I run it I get the error:

ERROR: /private/var/tmp/_bazel_justin/84cef48b5ae183d272bc73733d19e8e1/external/python_deps_pypi__flask/BUILD.bazel:11:11: no such package '@python_deps_pypi__itsdangerous//': The repository '@python_deps_pypi__itsdangerous' could not be resolved: Repository '@python_deps_pypi__itsdangerous' is not defined and referenced by '@python_deps_pypi__flask//:pkg'
ERROR: Analysis of target '//projects/my-python-app:main' failed; build aborted:

In my WORKSPACE I have:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

rules_python_version = "740825b7f74930c62f44af95c9a4c1bd428d2c53" # Latest @ 2021-06-23

http_archive(
    name = "rules_python",
    # Bazel will print the proper value to add here during the first build.
    # sha256 = "FIXME",
    strip_prefix = "rules_python-{}".format(rules_python_version),
    url = "https://github.com/bazelbuild/rules_python/archive/{}.zip".format(rules_python_version),
)

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

# Create a central repo that knows about the dependencies needed from
# requirements_lock.txt.
pip_parse(
   name = "python_deps",
   requirements_lock = "//third_party:requirements_lock.txt",
)
# Load the starlark macro which will define your dependencies.
load("@python_deps//:requirements.bzl", "install_deps")
# Call it to define repos for your requirements.
install_deps()

In my requirements_lock.txt I have:

Flask==2.0.2

In my BUILD I have:

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

py_binary(
    name = "main",
    srcs = ["main.py"],
    deps = [
        "//projects/calculator:calculator",
        requirement("Flask"),
    ],
)

Any ideas what's going on here? I was following the README in https://github.com/bazelbuild/rules_python

like image 988
jlcv Avatar asked Feb 03 '26 17:02

jlcv


1 Answers

You can use compile_pip_requirements to generate the requirements_lock.txt file.

  • Add it in the BUILD file -

    load("@rules_python//python:pip.bzl", "compile_pip_requirements")
    
    compile_pip_requirements(
        name = "requirements",
        requirements_in = "requirements.in",
        requirements_txt = "requirements_lock.txt",
    )
    
  • Write the base requirements in the requirements.in

    Flask==2.0.2
    
  • Run the following to generate the lock file

    bazel run requirements.update
    

    Note: initially you need to have the requirements_lock.txt file (empty file is okay), otherwise bezel will fail with file not found error

like image 155
fahad.shaon Avatar answered Feb 05 '26 06:02

fahad.shaon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!