导入caffe时出现ImportError: "No module named google.protobuf.internal"(import enum_type_wrapper)错误

21
我在电脑上安装了Anaconda的Python。当我启动Python解释器并在Python shell中输入"import caffe"时,会出现以下错误:
ImportError: No module named google.protobuf.internal

我有以下文件:

wire_format_lite_inl.h
wire_format_lite.h
wire_format.h
unknown_field_set.h
text_format.h
service.h
repeated_field.h
reflection_ops.h
message_lite.h
message.h
generated_message_util.h
extension_set.h
descriptor.proto
descriptor.h
generated_message_reflection.h
generated_enum_reflection.h
dynamic_message.h
descriptor.pb.h
descriptor_database.h

需要哪些文件才能使导入工作?是否需要一个名为“internal.h”的文件?


你是如何安装protobuf的?通过conda、apt-get还是从源代码编译安装?你是否也安装了protobuf的Python包? - ypx
谢谢您的回复。我根本没有安装protobuf。我将在我们的GitHub上查找Protobuf Python包。我不认为它在那里,但检查一下是值得的。如果没有的话,我已经请求将Google Protobuf从批准的软件列表中安装到我正在工作的操作系统上。 - PyNerd
@PyNerd,是的,你没有安装protobuf,tensorflow可能已经安装了它,但是版本不同,无法与caffe一起使用。因此,如果您卸载并重新安装它,它将安装最新版本的protobuf,这可能与tensorflow不兼容,但导入caffe将成功。通常,如果我们尝试安装特定版本的tensorflow,则会发生这种情况。 - H S Rathore
5个回答

36

可能是由于您的计算机上有两个 Python 环境所致,一个是由 Linux 发行版提供的(pip),另一个是 Anaconda 环境提供的(/home/username/anaconda2/bin/pip)。

尝试在两个环境中安装 protobuf 以确保成功

pip install protobuf

/home/username/anaconda2/bin/pip install protobuf


8
如果你正在使用Anaconda,那么请执行conda install protobuf命令进行安装。

7
如果您使用Ubuntu操作系统,请尝试使用以下命令安装protobuf:
sudo apt-get install protobuf

它解决了我遇到的相同问题。

16
这对我没有用,但 sudo apt-get install python-protobuf 有效。 - nkron
在我的情况下,运行 sudo apt install python3-protobuf 使其正常工作。 - Mariusz Pawelski

3

最简单的解决方法:

pip install grpcio
pip install protobuf

当需要使用多个Python版本时,建议按照aimuch的建议进行操作。
/usr/local/bin/pip2 install protobuf
/usr/local/bin/pip2 install grpcio

2
pip uninstall protobuf pip install protobuf 这对我起作用了。 - Unico
是的,可能grpcio与问题无关。 - H S Rathore
对我而言,使用 pip 导致了进一步的问题(开始要求我降级 protobuf)。正如另一个答案中所指出的那样,conda install protobuf 完美地解决了这个问题。 - user3342981

1
这是因为Python环境混乱。
# check where pip2
$ where pip2
/usr/local/bin/pip2
/usr/bin/pip2

# check where pip
$ which pip
/usr/local/bin/pip

在我的计算机上,我有两个pip2(我使用python2环境安装caffe),因此我使用/usr/local/bin/pip2 install protobuf 解决了这个问题。
/usr/local/bin/pip2 install protobuf

这样解决。/usr/local/bin/pip2安装protobuf。 - Ammad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接