如何在Bazel中添加来自Git的Python包作为依赖
在Bazel中构建未发布到PyPI的Python包(nvdiffrast)
你已经通过http_archive拉取了nvdiffrast的源码,接下来需要借助rules_python来基于setup.py构建这个包,具体步骤如下:
1. 引入rules_python规则
首先在WORKSPACE文件中添加rules_python依赖,确保能处理Python包和C/CUDA扩展的构建:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # 引入rules_python http_archive( name = "rules_python", sha256 = "f84bf656c22957d0d7728e933302e00a0d3d1578b899d379cfb1f216a4d9c487", url = "https://github.com/bazelbuild/rules_python/releases/download/0.25.0/rules_python-0.25.0.tar.gz", ) # 注册Python工具链 load("@rules_python//python:repositories.bzl", "python_register_toolchains") python_register_toolchains( name = "python3_10", python_version = "3.10", ) # 你的nvdiffrast源码拉取配置 http_archive( name = "nvdiffrast", url = "https://github.com/NVlabs/nvdiffrast/archive/refs/tags/v0.3.1.tar.gz", integrity = "sha256:替换为实际的哈希值", strip_prefix = "nvdiffrast-0.3.1", )
2. 为nvdiffrast添加BUILD配置
nvdiffrast包含C/CUDA扩展,需要编写BUILD.bazel定义构建规则。推荐在本地项目中创建配置文件,比如third_party/nvdiffrast/BUILD.bazel,内容如下:
load("@rules_python//python:defs.bzl", "py_library", "py_extension") load("@cuda//:defs.bzl", "cuda_library") # 需提前引入CUDA规则以支持GPU编译 # 构建CUDA/C++扩展 py_extension( name = "_nvdiffrast", srcs = glob([ "nvdiffrast/*.cpp", "nvdiffrast/*.cu", ]), hdrs = glob([ "nvdiffrast/*.h", ]), deps = [ "@cuda//:cuda_headers", ], copts = [ "-std=c++11", "-DNVDR_TORCH", # 基于PyTorch使用时添加该宏定义 ], linkopts = [ "-L/usr/local/cuda/lib64", "-lcudart", ], ) # 定义Python库,关联编译好的扩展 py_library( name = "nvdiffrast", srcs = glob([ "nvdiffrast/*.py", "setup.py", ]), data = [":_nvdiffrast"], imports = ["."], visibility = ["//visibility:public"], deps = [ # 添加nvdiffrast依赖的其他Python包,比如PyTorch "@pytorch//:torch", ], )
然后在WORKSPACE的http_archive中添加build_file参数,指向上述配置:
http_archive( name = "nvdiffrast", url = "https://github.com/NVlabs/nvdiffrast/archive/refs/tags/v0.3.1.tar.gz", integrity = "sha256:替换为实际的哈希值", strip_prefix = "nvdiffrast-0.3.1", build_file = "//third_party/nvdiffrast:BUILD.bazel", )
3. 在项目中依赖该包
在你的Python目标(如py_binary或py_library)中,直接添加依赖即可:
py_binary( name = "my_app", srcs = ["main.py"], deps = [ "@nvdiffrast//:nvdiffrast", ], )
注意事项
- 若需CUDA支持,要确保Bazel能识别CUDA环境,需额外引入CUDA相关规则并配置路径
- 编译选项需根据本地环境调整,比如CUDA安装路径、C++标准版本等
- 若nvdiffrast依赖其他PyPI包,用
rules_python的pip_parse或pip_install统一管理
内容的提问来源于stack exchange,提问作者zlenyk
相关产品推荐
相关产品推荐

