Python 导入 caffe 导致导入错误:“没有名为 google.protobuf.internal 的模块”(导入 enum_type_wrapper)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37666241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:46:10  来源:igfitidea点击:

Importing caffe results in ImportError: "No module named google.protobuf.internal" (import enum_type_wrapper)

pythoncaffeprotocol-buffers

提问by PyNerd

I installed Anaconda Python on my machine. When I start the Python Interpreter and type "import caffe" in the Python shell, I get the following error:

我在我的机器上安装了 Anaconda Python。当我启动 Python 解释器并在 Python shell 中键入“import caffe”时,出现以下错误:

ImportError: No module named google.protobuf.internal

I have the following files:

我有以下文件:

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

What files do I need so the import will work? Is there an "internal.h" file that is required?

我需要什么文件才能导入?是否需要“internal.h”文件?

回答by Jayant Agrawal

This is probably because you have two python environments in your machine, the one provided by your linux distribution(pip) and the other by the anaconda environment (/home/username/anaconda2/bin/pip).

这可能是因为您的机器中有两个 python 环境,一个由您的 linux 发行版 ( pip) 提供,另一个由 anaconda 环境 ( /home/username/anaconda2/bin/pip) 提供。

Try installing protobuf for both environments to be sure

尝试为两种环境安装 protobuf 以确保

pip install protobuf

pip install protobuf

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

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

回答by Michael Jaison G

If you are using Ubuntu, try installing protobuf using

如果您使用的是 Ubuntu,请尝试使用安装 protobuf

sudo apt-get install protobuf

It solved the same problem that I faced.

它解决了我面临的同样问题。

回答by chinmay singh

If you are using Anaconda, do conda install protobuf

如果您使用的是 Anaconda,请执行 conda install protobuf

回答by aimuch

This is because the python envirnment confusion.

这是因为python环境混乱。

# check where pip2
$ where pip2
/usr/local/bin/pip2
/usr/bin/pip2

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

On my computer, I have two pip2(I install caffe using python2 env), so I used /usr/local/bin/pip2 install protobufsolved this problem.

在我的电脑上,我有两个 pip2(我使用 python2 env 安装 caffe),所以我用/usr/local/bin/pip2 install protobuf解决了这个问题。

/usr/local/bin/pip2 install protobuf

回答by Ammad

Easiest way to fix it:

最简单的修复方法:

pip install grpcio
pip install protobuf

When have multiple python version use: as suggested by aimuch.

当有多个 python 版本使用时:按照aimuch的建议。

/usr/local/bin/pip2 install protobuf
/usr/local/bin/pip2 install grpcio