【最新解决方案】ImportError: cannot import name ‘builder‘ from ‘google.protobuf.internal‘

【报错原因】

  • 问题源自所用的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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值