You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Bazel编译TensorFlow源码时遇‘json未定义’错误的解决求助

解决Bazel编译TensorFlow时的json is not defined错误

编译TensorFlow r2.11分支时遇到以下错误:

io_bazel_rules_go/go/private/sdk.bzl:337:12: name 'json' is not defined

手动在代码中添加import json无法解决问题,因为这不是Python代码的问题,而是Bazel的Go规则(io_bazel_rules_go)中的Starlark配置代码缺少json模块导入。

操作步骤回顾

  • 克隆TensorFlow仓库:
    git clone https://github.com/tensorflow/tensorflow.git
    
  • 执行配置命令:
    PYTHON_VERSION=python3.10
    PYTHON_BIN_PATH=$(which $PYTHON_VERSION) \
    PYTHON_LIB_PATH=$($PYTHON_VERSION -c "import pip; print(pip.__path__[0].rstrip('/pip'))") \
    TF_NEED_CUDA=0 \
    TF_NEED_ROCM=0 \
    TF_DOWNLOAD_CLANG=0 \
    CC_OPT_FLAGS="-march=native -Wno-sign-compare -mnoavx" \
    TF_SET_ANDROID_WORKSPACE=0 \
    TF_ENABLE_XLA=1 \
    TF_NEED_OPENCL_SYCL=0 \
    TF_NEED_MPI=0 \
    ./configure
    
  • 执行编译:bazel build

环境信息

  • Ubuntu 22.04 LTS(所有安装包为最新版本)
  • Bazel 3.5.1- (@non-git)
  • TensorFlow分支:r2.11

解决方案

方法1:更新io_bazel_rules_go版本

  1. 打开TensorFlow仓库根目录下的WORKSPACE文件,找到io_bazel_rules_go的http_archive配置块。
  2. 将其替换为修复该问题的版本(如v0.37.0),同时更新对应的sha256校验值:
    http_archive(
        name = "io_bazel_rules_go",
        sha256 = "a8a789e6449f7f54a52e473497b497f9605b43e38d64b4b9509f94137075ca5b",
        urls = [
            "https://github.com/bazelbuild/rules_go/releases/download/v0.37.0/rules_go-v0.37.0.zip",
        ],
    )
    
  3. 同步更新bazel_gazelle到兼容版本(如v0.28.0):
    http_archive(
        name = "bazel_gazelle",
        sha256 = "d6f20f0a7607498175f5d00a6e70a9e0c103a786c98c094776df48e131f0f002",
        urls = [
            "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.zip",
        ],
    )
    
  4. 清除Bazel缓存并重新编译:
    bazel clean --expunge
    ./configure
    bazel build
    

方法2:升级Bazel版本

Bazel 3.5.1版本较旧,与TensorFlow r2.11及新版本rules_go存在兼容性问题。建议升级到TensorFlow r2.11推荐的Bazel版本(如5.3.0),之后重新执行配置和编译步骤。

内容的提问来源于stack exchange,提问作者krzydyn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:55:14