【问题标题】:ImportError: cannot import name 'builder' from 'google.protobuf.internal'ImportError: 无法从 \'google.protobuf.internal\' 导入名称 \'builder\'
【发布时间】:2022-08-16 16:47:27
【问题描述】:

我正在关注这个视频text 这段代码发现错误,22:22

.

!python {SCRIPTS_PATH + \'/generate_tfrecord.py\'} -x {IMAGE_PATH + \'/train\'} -l {ANNOTATION_PATH + \'/label_map.pbtxt\'} -o {ANNOTATION_PATH + \'/train.record\'}
!python {SCRIPTS_PATH + \'/generate_tfrecord.py\'} -x{IMAGE_PATH + \'/test\'} -l {ANNOTATION_PATH + \'/label_map.pbtxt\'} -o {ANNOTATION_PATH + \'/test.record\'}

. 发布确切的错误消息,

Traceback (most recent call last):
  File \"D:\\info\\1 Master\\2 semster\\RealTimeObjectDetection-main\\Tensorflow\\scripts\\generate_tfrecord.py\", line 29, in <module>
    from object_detection.utils import dataset_util, label_map_util
  File \"C:\\Users\\bachir\\PycharmProjects\\pythonProject\\venv\\lib\\site-packages\\object_detection\\utils\\label_map_util.py\", line 29, in <module>
    from object_detection.protos import string_int_label_map_pb2
  File \"C:\\Users\\bachir\\PycharmProjects\\pythonProject\\venv\\lib\\site-packages\\object_detection\\protos\\string_int_label_map_pb2.py\", line 5, in <module>
    from google.protobuf.internal import builder as _builder
ImportError: cannot import name \'builder\' from \'google.protobuf.internal\' (C:\\Users\\bachir\\PycharmProjects\\pythonProject\\venv\\lib\\site-packages\\google\\protobuf\\internal\\__init__.py)

.

首先我尝试下载一些丢失的包但同样的问题,我想帮忙

    标签: python


    【解决方案1】:

    按着这些次序:

    1. 安装最新的protobuf(我的例子是4.21.1)

      pip install protobuf

      1. builder.py...\Lib\site-packages\google\protobuf\internal 复制到您的计算机(比如说“文档”)
      2. 安装与您的项目兼容的 protobuf,对我来说是 3.19.4

      pip install protobuf==3.19.4

      1. 将 builder.py 从(比如说“文档”)复制到 Lib\site-packages\google\protobuf\internal
      2. 运行您的代码

    【讨论】:

    • 哇聪明的解决方案!
    • 你刚刚保存了我的显示器和键盘。我正在阅读本教程 (tensorflow-object-detection-api-tutorial.readthedocs.io/en/…)。最后,您的解决方案有效!
    • protobuf 是这个世界上最糟糕的软件,每个人都依赖它,每个版本都破坏了它自己的 API。
    【解决方案2】:

    您需要升级到最新版本的protobuf 软件包:

    pip install --upgrade protobuf

    原因是 Python 类从 Protobuf v3.20.0 开始被简化。直接来自release notes 它说:

    Protobuf python 生成的代码被简化了。描述符和 消息类的定义现在是动态创建的 内部/builder.py。消息类的插入点是 丢弃。

    这就解释了为什么生成的 Python 代码现在引用了 builder 模块,如果您还没有更新到最新版本的 protobuf 包,它就找不到该模块。这在发行说明中没有解释,但我验证自己如果升级protobuf 包它可以工作。

    【讨论】:

      【解决方案3】:

      我可以通过不使用最新版本的 protoc 编译器而是使用旧版本 v3.19.4(请参阅https://github.com/protocolbuffers/protobuf/releases)来编译我的 .proto 文件来解决这个问题。

      【讨论】:

        【解决方案4】:

        因为descriptor_pb2.py是从protoc生成的。所以你需要保持你的构建系统和运行系统之间的兼容性。

        一言以蔽之,确保您的 protoc 的版本小于或等于 protobuf 的版本。

        仅供参考,您可以直接从“https://github.com/protocolbuffers/protobuf/releases”下载相应的协议

        【讨论】:

          【解决方案5】:

          从您安装 python 的位置复制 builder.py 文件,或者如果您使用 ananconda 可以将 C:\Users\bachir\anaconda3\Lib\site-packages\google\protobuf\internal 中的 builder.py 复制,然后将其粘贴到 C:\用户\bachir\PycharmProjects\pythonProject\venv\lib\site-packages\google\protobuf\internal

          【讨论】:

            【解决方案6】:

            分两步解决:

            1. pip 卸载 protobuf

            2. pip install protobuf==4.21

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-01-22
              • 2021-06-30
              • 2020-10-19
              • 2018-08-13
              • 2015-01-26
              • 2020-10-10
              • 2020-02-12
              • 2021-11-27
              相关资源
              最近更新 更多