Python 导入错误:没有名为 google.protobuf 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38680593/
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
ImportError: No module named google.protobuf
提问by Lin Ma
I am following this guide (https://developers.google.com/protocol-buffers/docs/pythontutorial) and using the exact sample of addressbook.proto.
我正在遵循本指南(https://developers.google.com/protocol-buffers/docs/pythontutorial)并使用 addressbook.proto 的确切示例。
Post the content of compiler generated addressbook_pb2.py file as well. When I run the following simple program, there is error saying, cannot find google.protobuf, any ideas how to resolve this issue? Thanks.
将编译器生成的 addressbook_pb2.py 文件的内容也贴出来。当我运行以下简单程序时,出现错误提示,找不到 google.protobuf,任何想法如何解决此问题?谢谢。
BTW, using Python 2.7 on Mac OSX.
顺便说一句,在 Mac OSX 上使用 Python 2.7。
from addressbook_pb2 import Person
p = Person()
p.email = "abc"
print p.email
Here is the automated generated file addressbook_pb2.py,
这是自动生成的文件addressbook_pb2.py,
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: addressbook.proto
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='addressbook.proto',
package='tutorial',
syntax='proto2',
serialized_pb=_b('\n\x11\x61\x64\x64ressbook.proto\x12\x08tutorial\"\xda\x01\n\x06Person\x12\x0c\n\x04name\x18\x01 \x02(\t\x12\n\n\x02id\x18\x02 \x02(\x05\x12\r\n\x05\x65mail\x18\x03 \x01(\t\x12+\n\x05phone\x18\x04 \x03(\x0b\x32\x1c.tutorial.Person.PhoneNumber\x1aM\n\x0bPhoneNumber\x12\x0e\n\x06number\x18\x01 \x02(\t\x12.\n\x04type\x18\x02 \x01(\x0e\x32\x1a.tutorial.Person.PhoneType:\x04HOME\"+\n\tPhoneType\x12\n\n\x06MOBILE\x10\x00\x12\x08\n\x04HOME\x10\x01\x12\x08\n\x04WORK\x10\x02\"/\n\x0b\x41\x64\x64ressBook\x12 \n\x06person\x18\x01 \x03(\x0b\x32\x10.tutorial.Person')
)
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
_PERSON_PHONETYPE = _descriptor.EnumDescriptor(
name='PhoneType',
full_name='tutorial.Person.PhoneType',
filename=None,
file=DESCRIPTOR,
values=[
_descriptor.EnumValueDescriptor(
name='MOBILE', index=0, number=0,
options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='HOME', index=1, number=1,
options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='WORK', index=2, number=2,
options=None,
type=None),
],
containing_type=None,
options=None,
serialized_start=207,
serialized_end=250,
)
_sym_db.RegisterEnumDescriptor(_PERSON_PHONETYPE)
_PERSON_PHONENUMBER = _descriptor.Descriptor(
name='PhoneNumber',
full_name='tutorial.Person.PhoneNumber',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='number', full_name='tutorial.Person.PhoneNumber.number', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='type', full_name='tutorial.Person.PhoneNumber.type', index=1,
number=2, type=14, cpp_type=8, label=1,
has_default_value=True, default_value=1,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=128,
serialized_end=205,
)
_PERSON = _descriptor.Descriptor(
name='Person',
full_name='tutorial.Person',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='name', full_name='tutorial.Person.name', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='id', full_name='tutorial.Person.id', index=1,
number=2, type=5, cpp_type=1, label=2,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='email', full_name='tutorial.Person.email', index=2,
number=3, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='phone', full_name='tutorial.Person.phone', index=3,
number=4, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[_PERSON_PHONENUMBER, ],
enum_types=[
_PERSON_PHONETYPE,
],
options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=32,
serialized_end=250,
)
_ADDRESSBOOK = _descriptor.Descriptor(
name='AddressBook',
full_name='tutorial.AddressBook',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='person', full_name='tutorial.AddressBook.person', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=252,
serialized_end=299,
)
_PERSON_PHONENUMBER.fields_by_name['type'].enum_type = _PERSON_PHONETYPE
_PERSON_PHONENUMBER.containing_type = _PERSON
_PERSON.fields_by_name['phone'].message_type = _PERSON_PHONENUMBER
_PERSON_PHONETYPE.containing_type = _PERSON
_ADDRESSBOOK.fields_by_name['person'].message_type = _PERSON
DESCRIPTOR.message_types_by_name['Person'] = _PERSON
DESCRIPTOR.message_types_by_name['AddressBook'] = _ADDRESSBOOK
Person = _reflection.GeneratedProtocolMessageType('Person', (_message.Message,), dict(
PhoneNumber = _reflection.GeneratedProtocolMessageType('PhoneNumber', (_message.Message,), dict(
DESCRIPTOR = _PERSON_PHONENUMBER,
__module__ = 'addressbook_pb2'
# @@protoc_insertion_point(class_scope:tutorial.Person.PhoneNumber)
))
,
DESCRIPTOR = _PERSON,
__module__ = 'addressbook_pb2'
# @@protoc_insertion_point(class_scope:tutorial.Person)
))
_sym_db.RegisterMessage(Person)
_sym_db.RegisterMessage(Person.PhoneNumber)
AddressBook = _reflection.GeneratedProtocolMessageType('AddressBook', (_message.Message,), dict(
DESCRIPTOR = _ADDRESSBOOK,
__module__ = 'addressbook_pb2'
# @@protoc_insertion_point(class_scope:tutorial.AddressBook)
))
_sym_db.RegisterMessage(AddressBook)
# @@protoc_insertion_point(module_scope)
Edit 1,
编辑 1,
Tried pip install protobuf
, met with the following error,
试过了pip install protobuf
,遇到以下错误,
Requirement already satisfied (use --upgrade to upgrade): protobuf in /Users/foo/miniconda2/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.9 in /Users/foo/miniconda2/lib/python2.7/site-packages/six-1.10.0-py2.7.egg (from protobuf)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/foo/miniconda2/lib/python2.7/site-packages (from protobuf)
Here is the output of python version,
这是python版本的输出,
python -V
Python 2.7.11 :: Continuum Analytics, Inc.
**Edit 2 **
**编辑 2 **
Post exact error message,
发布确切的错误消息,
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1531, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 938, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/foo/personal/featureExtraction/protobuf_test.py", line 1, in <module>
from addressbook_pb2 import Person
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_monkey_qt.py", line 71, in patched_import
return original_import(name, *args, **kwargs)
File "/Users/foo/personal/featureExtraction/addressbook_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_monkey_qt.py", line 71, in patched_import
return original_import(name, *args, **kwargs)
ImportError: No module named google.protobuf
Edit 3,
编辑 3,
error message when import google
,
错误消息时import google
,
Edit 4,
编辑 4,
Output of which pip
,
的输出which pip
,
which pip
/Users/foo/miniconda2/bin/pip
Output of sys.executable
,
的输出sys.executable
,
/Users/foo/anaconda/bin/python
/用户/foo/anaconda/bin/python
Edit 5,
编辑 5,
foo-mn1:featureExtraction foo$ sudo /Users/foo/miniconda2/bin/pip install protobuf
Password:
The directory '/Users/foo/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/foo/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): protobuf in /Users/foo/miniconda2/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.9 in /Users/foo/miniconda2/lib/python2.7/site-packages/six-1.10.0-py2.7.egg (from protobuf)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/foo/miniconda2/lib/python2.7/site-packages (from protobuf)
foo-mn1:featureExtraction foo$ sudo /Users/foo/miniconda2/bin/pip install google
The directory '/Users/foo/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/foo/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): google in /Users/foo/miniconda2/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4 in /Users/foo/miniconda2/lib/python2.7/site-packages (from google)
采纳答案by Marl
Had the same issue and I resolved it by using :
有同样的问题,我通过使用解决了它:
conda install protobuf
回答by David Gomes
回答by vinit payal
The reason of this would be mostly below evilcommand pip install google
. I was facing issue for google-cloud
the same steps are true for protobuf
also as protobuf
also get installed in google > protobuf
namespace.
造成这种情况的原因主要是在邪恶的命令之下pip install google
。我面临的问题google-cloud
是同样的步骤也是如此,protobuf
因为protobuf
也安装在google > protobuf
命名空间中。
If you have executed this command like me then you are at correct place actually google
package is not owned by google which can be confirmed by command pip show google
output of which should be somewhat as below
如果你像我一样执行了这个命令,那么你在正确的地方实际上google
包不是由谷歌拥有的,这可以通过命令pip show google
输出来确认,它应该有点如下
Name: google
Version: 1.9.3
Summary: Python bindings to the Google search engine.
Home-page: http://breakingcode.wordpress.com/
Author: Mario Vilas
Author-email: [email protected]
License: UNKNOWN
Location: <Path where this package is installed>
Requires: beautifulsoup4
So because of above google
namespace is reserved for this package and coincidentally google-cloud
also expects namespace google > cloud
and it results in collision of namespace of these two packages.
所以因为上面的google
命名空间是为这个包保留的,巧合的是google-cloud
也期望命名空间google > cloud
,这导致这两个包的命名空间发生冲突。
See in below screenshot namespace of google-protobuf
as google > protobuf
请参见下面的屏幕截图命名空间google-protobuf
asgoogle > protobuf
Solution:- Unofficial google
package need to be uninstalled which can be done by using pip uninstall google
after this you can reinstall google-cloud
using pip install google-cloud
or protobuf
using pip install protobuf
解决方案:-google
需要卸载非官方包,可以通过使用pip uninstall google
完成此操作之后您可以重新安装google-cloud
使用pip install google-cloud
或protobuf
使用pip install protobuf
FootNotes :- Assuming you have installed unofficial google
package by mistake and you don't actually need it along with google-cloud
package. If you need both unofficial google
and google-cloud
above solution won't work.
FootNotes :- 假设您google
错误地安装了非官方软件包,并且您实际上并不需要它与package.json一起使用google-cloud
。如果您同时需要非官方google
和google-cloud
上述解决方案将不起作用。
Let me know if this solves your particular issue.
如果这能解决您的特定问题,请告诉我。
回答by Bruce Matichuk
On Mac OS - Installing tensorflow 1.3 - it will automatically remove other protobuf installs and install protobuf 3.4. However, this does not work and neither does installing or downgrading to any other protobuf version.
在 Mac OS 上 - 安装 tensorflow 1.3 - 它将自动删除其他 protobuf 安装并安装 protobuf 3.4。但是,这不起作用,安装或降级到任何其他 protobuf 版本也不起作用。
However I found a solution. Not sure why this works - but on Mac OS this solved it.
不过我找到了解决办法。不知道为什么会这样 - 但在 Mac OS 上这解决了它。
pip install google
回答by Jerryhtz
I also have this issue and have been looking into it for a long time. It seems that there is no such problem on python 3+. The problem is actually on google.protobuf
我也有这个问题,研究了很久。在python 3+上似乎没有这样的问题。问题实际上出在 google.protobuf 上
Solution 1:
解决方案1:
pip uninstall protobuf
pip uninstall google
pip install google
pip install protobuf
pip install google-cloud
Solution 2:
解决方案2:
create an __init__.py
in "google" folder.
__init__.py
在“谷歌”文件夹中创建一个。
cd /path/to/your/env/lib/python2.7/site-packages/google
touch __init__.py
Hopefully it will work.
希望它会起作用。
回答by janos
When pip
tells you that you already have protobuf
,
but PyCharm (or other) tells you that you don't have it,
it means that pip
and PyCharm are using a different Python interpreter.
This is a very common issue, especially on a Mac, with no standard Python package management.
当pip
告诉您您已经拥有protobuf
,但 PyCharm(或其他)告诉您没有它时,这意味着pip
和 PyCharm 正在使用不同的 Python 解释器。这是一个非常常见的问题,尤其是在没有标准 Python 包管理的 Mac 上。
The best way to completely eliminate such issues is using a virtualenv
per Python project, which is essentially a directory of Python packages and environment variable settings to isolate the Python environment of the project from everything else.
完全消除此类问题的最佳方法是使用virtualenv
每个 Python 项目,它本质上是一个 Python 包和环境变量设置的目录,用于将项目的 Python 环境与其他所有内容隔离开来。
Create a virtualenv
for your project like this:
virtualenv
为您的项目创建一个这样的:
cd project
virtualenv --distribute virtualenv -p /path/to/python/executable
This creates a directory called virtualenv
inside your project.
(Make sure to configure your VCS (for example Git) to ignore this directory.)
这会virtualenv
在您的项目中创建一个名为的目录。(确保将您的 VCS(例如 Git)配置为忽略此目录。)
To install packages in this virtualenv
, you need to activate the environment variable settings:
要在此安装软件包virtualenv
,您需要激活环境变量设置:
. virtualenv/bin/activate
Verify that pip
will use the right Python executable inside the virtualenv
, by running pip -V
. It should tell you the Python library path used, which should be inside the virtualenv
.
验证pip
会使用正确的Python可执行程序内virtualenv
,通过运行pip -V
。它应该告诉你使用的 Python 库路径,它应该在virtualenv
.
Now you can use pip
to install protobuf
as you did.
现在您可以使用pip
来安装protobuf
。
And finally, you need to make PyCharm use this virtualenv
instead of the system libraries. Somewhere in the project settings you can configure an interpreter for the project, select the Python executable inside the virtualenv
.
最后,您需要让 PyCharm 使用它virtualenv
而不是系统库。在项目设置的某处,您可以为项目配置解释器,选择virtualenv
.
回答by Hymany Wang
I encountered the same situation. And I find out it is because the pip should be updated. It may be the same reason for your problem.
我遇到了同样的情况。我发现这是因为 pip 应该更新。这可能与您的问题的原因相同。
回答by Ohad Cohen
I got the same error message as in the title, but in my case import google
was working and import google.protobuf
wasn't (on python3.5, ubuntu 16.04).
我收到了与标题相同的错误消息,但就我而言import google
,它可以正常工作,但import google.protobuf
不能正常工作(在 python3.5、ubuntu 16.04 上)。
It turned out that I've installed python3-google-apputils
package (using apt) and it was installed to '/usr/lib/python3/dist-packages/google/apputils/', while protobuf (which was installed using pip) was in "/usr/lib/python3.5/dist-packages/google/protobuf/" - and it was a "google" namespace collapse.
原来我已经安装了python3-google-apputils
包(使用 apt)并将其安装到“/usr/lib/ python3/dist-packages/google/apputils/”,而 protobuf(使用 pip 安装)在“/usr /lib/ python3.5/dist-packages/google/protobuf/” - 这是一个“google”命名空间崩溃。
Uninstalling google-apputils (from apt, and reinstalling it using pip) solved the problem.
卸载 google-apputils(从 apt,然后使用 pip 重新安装)解决了这个问题。
sudo apt remove python3-google-apputils
sudo pip3 install google-apputils
回答by Tails
I had this issuewhen using the Python wrapper for DGraph DB, which was somehow fixed by this commit(perhaps of use to someone).
我在使用 DGraph DB 的 Python 包装器时遇到了这个问题,这个提交以某种方式修复了这个问题(可能对某人有用)。
回答by Jaskaran singh Rajal
if protobuf is installed then import it like this
如果安装了protobuf,则像这样导入
pip install protobuf
import google.protobuf