【报错原因】
- 问题源自所用的google.protobuf版本过低。在更新的版本里,builder模块已经被迁移到了google.protobuf包内,不再位于google.protobuf.internal路径下。
- 环境版本不兼容问题(我找60%的bug都是兼容问题😢)
- 市面上大部分解决方案都是围绕更新protobuf版本(常规思路)
- google 命名空间的命名空间冲突有关。
我前面四种方案都失效了,(也是网上常见给出的方法),若相同者可以直接跳到最下面方案🔥
1、解决方案一
直接更新为最新版本,或者相对应的版本
pip install --upgrade protobuf
pip install protobuf==3.19
2、解决方案二
问题都与“google”命名空间的命名空间冲突有关。
pip uninstall protobuf
pip uninstall google
pip install google
pip install protobuf
import google.protobuf
3、解决方案三
在anaconda 上操作
conda install -c anaconda protobuf
4、解决方案四
- 缺啥补啥
- 在/path/to/your/env/lib/python2.7(自己文件的目录)/site-packages/google下新建一个
__init__.py
5、解决方案五(我的方案,最终已解决)😄
- 缺啥补啥,既然在相关目录下找不到
import builder as _builder
,则直接填充下面代码作为builder.py
注意要在正确的目录下,即为报错信息显示的目录下
# Protocol Buffers - Google's data interchange format
# Copyright 2008 Google Inc. All rights reserved.
# https://developers.google.com/protocol-buffers/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, IN