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
尝试
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,
编辑4,
1 2 | which pip /Users/foo/miniconda2/bin/pip |
/用户/ 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脚本。
按照此链接。
其原因主要是在邪恶命令
如果您像我一样执行了此命令,那么您位于正确的位置,实际上
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 |
因此,由于上述
在下面的
解决方案:-需要卸载非官方的
脚注:-假设您错误地安装了非官方的
让我知道这是否可以解决您的特定问题。
在Mac OS上-安装tensorflow 1.3-它会自动删除其他protobuf安装并安装protobuf 3.4。但是,这不起作用,安装或降级到任何其他protobuf版本也不行。
但是我找到了解决方案。
不知道为什么会这样-但是在Mac OS上就解决了。
1 | pip install google |
当
但是PyCharm(或其他)告诉您您没有它,
这意味着
这是一个非常常见的问题,尤其是在没有标准Python软件包管理的Mac上。
彻底消除此类问题的最佳方法是使用每个Python项目
为您的项目创建一个
1 2 | cd project virtualenv --distribute virtualenv -p /path/to/python/executable |
这将在项目内部创建一个名为
(确保将您的VCS(例如,Git)配置为忽略此目录。)
要在此
1 | . virtualenv/bin/activate |
通过运行
现在,您可以像以前一样使用
最后,您需要使PyCharm使用此
我也有这个问题,并且已经研究了很长时间。 似乎在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"文件夹中创建一个
1 2 | cd /path/to/your/env/lib/python2.7/site-packages/google touch __init__.py |
希望它会起作用。
我得到了与标题相同的错误消息,但在我的情况下,
原来,我已经安装了
卸载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 |