【问题标题】:pip_install ignored when combined with py_image?与 py_image 结合使用时忽略 pip_install?
【发布时间】:2022-01-03 18:44:22
【问题描述】:

我正在尝试构建一个具有在 requirements.txt 中指定的依赖关系的“hello, world”Docker 映像。考虑以下文件:

工作空间

workspace(name = "bazel01_helloworld")

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

http_archive(
    name = "io_bazel_rules_docker",
    sha256 = "92779d3445e7bdc79b961030b996cb0c91820ade7ffa7edca69273f404b085d5",
    strip_prefix = "rules_docker-0.20.0",
    urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.20.0/rules_docker-v0.20.0.tar.gz"],
)

load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl",
    docker_toolchain_configure="toolchain_configure"
)

load(
    "@io_bazel_rules_docker//repositories:repositories.bzl",
    container_repositories = "repositories",
)

container_repositories()

load(
    "@io_bazel_rules_docker//python:image.bzl",
    _py_image_repos = "repositories",
)

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

pip_install(
   name = "my_deps",
   requirements = "requirements.txt",
)

_py_image_repos()

构建

load("@io_bazel_rules_docker//python:image.bzl", "py_image")
py_image(
    name = "helloWorld",
    srcs = ["helloWorld.py"],
    main = "helloWorld.py",
)

requirements.txt

flask

helloWorld.py

import flask
print('Hello, world!')

这是我得到的输出:

> sudo bazel run helloWorld
INFO: Analyzed target //:helloWorld (87 packages loaded, 7245 targets configured).
INFO: Found 1 target...
Target //:helloWorld up-to-date:
  bazel-bin/helloWorld-layer.tar
INFO: Elapsed time: 5.390s, Critical Path: 2.41s
INFO: 58 processes: 20 internal, 38 linux-sandbox.
INFO: Build completed successfully, 58 total actions
INFO: Build completed successfully, 58 total actions
Loaded image ID: sha256:370d944e74b0d70ff857130e877235fb921b2b9e3cb4038ac88fe119d8843380
Tagging 370d944e74b0d70ff857130e877235fb921b2b9e3cb4038ac88fe119d8843380 as bazel:helloWorld
Traceback (most recent call last):
  File "/app//helloWorld.binary.runfiles/bazel01_helloworld/helloWorld.py", line 1, in <module>
    import flask
ImportError: No module named flask

Flask 显然没有安装。我做错了什么?

【问题讨论】:

    标签: python docker pip bazel


    【解决方案1】:

    再一次,我通过 Bazel Slack 上的#getstarted 频道收到了用户 matt 的答复:

    py_image 目标中没有引用 pip 依赖项 https://github.com/bazelbuild/rules_docker#py_image

    这是一个工作示例:

    工作空间

    workspace(name = "bazel01_helloworld")
    
    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    http_archive(
        name = "rules_python",
        sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",
        urls = [
            "https://mirror.bazel.build/github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
            "https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
        ],
    )
    
    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    
    http_archive(
        name = "io_bazel_rules_docker",
        sha256 = "92779d3445e7bdc79b961030b996cb0c91820ade7ffa7edca69273f404b085d5",
        strip_prefix = "rules_docker-0.20.0",
        urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.20.0/rules_docker-v0.20.0.tar.gz"],
    )
    
    load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl",
        docker_toolchain_configure="toolchain_configure"
    )
    
    load(
        "@io_bazel_rules_docker//repositories:repositories.bzl",
        container_repositories = "repositories",
    )
    
    container_repositories()
    
    load(
        "@io_bazel_rules_docker//python3:image.bzl",
        _py_image_repos = "repositories",
    )
    
    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 = "my_deps",
       requirements_lock = "requirements.txt",
    )
    
    
    # Load the starlark macro which will define your dependencies.
    load("@my_deps//:requirements.bzl", "install_deps")
    # Call it to define repos for your requirements.
    install_deps()
    
    _py_image_repos()
    

    构建

    load("@io_bazel_rules_docker//python3:image.bzl", "py3_image")
    load("@my_deps//:requirements.bzl", "requirement")
    py3_image(
        deps = [requirement("flask")],
        name = "helloWorld",
        srcs = ["helloWorld.py"],
        main = "helloWorld.py",
    )
    

    external/requirements.txt

    click==8.0.3
    Flask==2.0.2
    itsdangerous==2.0.1
    Jinja2==3.0.3
    MarkupSafe==2.0.1
    Werkzeug==2.0.2
    

    helloWorld.py

    import flask
    print('Hello, world!')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-13
      • 2013-12-24
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多