关于python:ImportError:没有名为google.protobuf的模块

ImportError: No module named google.protobuf

我正在遵循本指南(https://developers.google.com/protocol-buffers/docs/pythontutorial)并使用addressbook.proto的确切示例。

还要发布编译器生成的addressbook_pb2.py文件的内容。
当我运行以下简单程序时,出现错误提示,找不到google.protobuf,有什么办法解决此问题? 谢谢。

顺便说一句,在Mac OSX上使用Python 2.7。

1
2
3
4
5
6
from addressbook_pb2 import Person

p = Person()
p.email ="abc"

print p.email

这是自动生成的文件addressbook_pb2.py,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# 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('
\x11\x61\x64\x64ressbook.proto\x12\x08tutorial"\xda\x01
\x06Person\x12\x0c
\x04name\x18\x01 \x02(\t\x12

\x02id\x18\x02 \x02(\x05\x12

\x05\x65mail\x18\x03 \x01(\t\x12+
\x05phone\x18\x04 \x03(\x0b\x32\x1c.tutorial.Person.PhoneNumber\x1aM
\x0bPhoneNumber\x12\x0e
\x06number\x18\x01 \x02(\t\x12.
\x04type\x18\x02 \x01(\x0e\x32\x1a.tutorial.Person.PhoneType:\x04HOME"+
\tPhoneType\x12

\x06MOBILE\x10\x00\x12\x08
\x04HOME\x10\x01\x12\x08
\x04WORK\x10\x02"/
\x0b\x41\x64\x64ressBook\x12
\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)

编辑1

尝试pip install protobuf,遇到以下错误,

1
2
3
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)

这是python版本的输出,

1
2
python -V
Python 2.7.11 :: Continuum Analytics, Inc.

**编辑2 **

发布确切的错误消息,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

编辑3,

import google时出现错误消息,

enter image description here

编辑4,

which pip的输出,

1
2
which pip
/Users/foo/miniconda2/bin/pip

sys.executable的输出,

/用户/ foo / anaconda / bin / python

编辑5

1
2
3
4
5
6
7
8
9
10
11
12
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)


遇到相同的问题,我通过使用解决了它:

1
conda install protobuf


您应该运行:

1
pip install protobuf

这将安装Google protobuf,然后您可以运行该Python脚本。

按照此链接。


其原因主要是在邪恶命令pip install google下。我遇到了google-cloud的问题,对于protobuf也是一样,因为protobuf也安装在google > protobuf命名空间中。

如果您像我一样执行了此命令,那么您位于正确的位置,实际上google包不是google拥有的,可以通过命令pip show google确认,其输出应如下所示

1
2
3
4
5
6
7
8
9
 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: mvilas@gmail.com
 License: UNKNOWN
 Location: <Path where this package is installed>
 Requires: beautifulsoup4

因此,由于上述google命名空间是为此程序包保留的,巧合的是google-cloud也希望使用命名空间google > cloud,这会导致这两个程序包的命名空间冲突。

在下面的google-protobuf屏幕快照命名空间中以google > protobuf看到

google-cloud namespace screenshot google >云>数据存储

解决方案:-需要卸载非官方的google软件包,这可以通过使用pip uninstall google完成,之后您可以使用pip install google-cloud重新安装google-cloud或使用pip install protobuf重新安装protobuf

脚注:-假设您错误地安装了非官方的google软件包,而实际上并不需要google-cloud软件包。如果您同时需要非官方的googlegoogle-cloud,则上述解决方案将不起作用。

让我知道这是否可以解决您的特定问题。


在Mac OS上-安装tensorflow 1.3-它会自动删除其他protobuf安装并安装protobuf 3.4。但是,这不起作用,安装或降级到任何其他protobuf版本也不行。

但是我找到了解决方案。
不知道为什么会这样-但是在Mac OS上就解决了。

1
pip install google


pip告诉您您已经有protobuf时,
但是PyCharm(或其他)告诉您您没有它,
这意味着pip和PyCharm使用不同的Python解释器。
这是一个非常常见的问题,尤其是在没有标准Python软件包管理的Mac上。

彻底消除此类问题的最佳方法是使用每个Python项目virtualenv,该项目本质上是Python软件包和环境变量设置的目录,用于将项目的Python环境与其他所有内容隔离开。

为您的项目创建一个virtualenv,如下所示:

1
2
cd project
virtualenv --distribute virtualenv -p /path/to/python/executable

这将在项目内部创建一个名为virtualenv的目录。
(确保将您的VCS(例如,Git)配置为忽略此目录。)

要在此virtualenv中安装软件包,您需要激活环境变量设置:

1
. virtualenv/bin/activate

通过运行pip -V,验证pip将在virtualenv中使用正确的Python可执行文件。它应该告诉您所使用的Python库路径,该路径应位于virtualenv内部。

现在,您可以像以前一样使用pip安装protobuf

最后,您需要使PyCharm使用此virtualenv而不是系统库。在项目设置中的某处,您可以为项目配置解释器,然后选择virtualenv中的Python可执行文件。


我也有这个问题,并且已经研究了很长时间。 似乎在python 3+上没有这样的问题。 问题实际上出在google.protobuf上

解决方案1:

1
2
3
4
5
pip uninstall protobuf
pip uninstall google
pip install google
pip install protobuf
pip install google-cloud

解决方案2:

在" google"文件夹中创建一个__init__.py

1
2
cd /path/to/your/env/lib/python2.7/site-packages/google
touch __init__.py

希望它会起作用。


我得到了与标题相同的错误消息,但在我的情况下,import google正常工作,而import google.protobuf无效(在python3.5,ubuntu 16.04上)。

原来,我已经安装了python3-google-apputils软件包(使用apt),并且已将其安装到" / usr / lib / python3 / dist-packages / google / apputils /"中,而protobuf(使用pip安装了)" /usr/lib/python3.5/dist-packages/google/protobuf/"-这是" google"命名空间折叠。

卸载google-apputils(从apt并使用pip重新安装)解决了该问题。

1
2
sudo apt remove python3-google-apputils
sudo pip3 install google-apputils

我遇到了同样的情况。我发现这是因为应该更新点子。可能是您遇到问题的原因相同。


在将Python包装器用于DGraph DB时遇到了这个问题,该提交在某种程度上已得到修复(可能对某人使用)。


如果安装了protobuf,则以这种方式导入

1
2
3
pip install protobuf

import google.protobuf