ImportError:没有名为google.protobuf

时间:2016-07-31 03:51:48

标签: python python-2.7 protocol-buffers

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

发布编译器生成的addressbook_pb2.py文件的内容。 当我运行以下简单程序时,有错误说,找不到google.protobuf,任何想法如何解决这个问题?感谢。

BTW,在Mac OSX上使用Python 2.7。

from addressbook_pb2 import Person

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

print p.email

以下是自动生成的文件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)

修改1

尝试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)

这是python版本的输出,

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

**编辑2 **

发布确切的错误消息,

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

的输出
which pip
/Users/foo/miniconda2/bin/pip

sys.executable

的输出

/用户/富/安纳康达/ bin中/蟒

编辑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)

13 个答案:

答案 0 :(得分:25)

你应该跑:

pip install protobuf

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

根据this link

答案 1 :(得分:23)

有同样的问题,我使用以下方法解决了这个问题:

conda install protobuf

答案 2 :(得分:8)

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

如果你像我一样执行了这个命令,那么你的位置正确google包不属于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: mvilas@gmail.com
 License: UNKNOWN
 Location: <Path where this package is installed>
 Requires: beautifulsoup4

因为上面的google命名空间是为这个包保留的,巧合google-cloud也期望命名空间google > cloud,它会导致这两个包的命名空间冲突。

请参阅以下屏幕截图名称空间google-protobufgoogle > protobuf

google-cloud namespace screenshot google > cloud > datastore

解决方案: - 需要卸载非正式的google软件包,可以使用pip uninstall google完成此操作,之后您可以使用{{1}重新安装google-cloud }或pip install google-cloud使用protobuf

FootNotes: - 假设您错误地安装了非正式的pip install protobuf软件包,并且实际上并不需要它与google软件包一起使用。如果您需要非正式的google-cloudgoogle以上解决方案,那么就无法工作。

如果这可以解决您的特定问题,请告诉我。

答案 3 :(得分:7)

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

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

<script src="https://unpkg.com/rxjs@5/bundles/Rx.min.js"></script>

答案 4 :(得分:5)

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

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

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

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

这会在项目中创建一个名为virtualenv的目录。 (确保配置VCS(例如Git)忽略此目录。)

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

. virtualenv/bin/activate

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

现在,您可以使用pip安装protobuf

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

答案 5 :(得分:1)

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

答案 6 :(得分:1)

我收到了与标题中相同的错误消息,但在我的情况下import google正在运行且import google.protobuf没有(在python3.5,ubuntu 16.04上)。

事实证明,我已经安装了python3-google-apputils包(使用apt),并且已安装到&#39; / usr / lib / python3 / dist-packages / google / apputils /&#39;,而protobuf(使用pip安装)位于&#34; / usr / lib / python3.5 / dist-packages / google / protobuf /&# 34; - 这是一个&#34; google&#34;命名空间崩溃。

卸载google-apputils(来自apt,并使用pip重新安装)解决了这个问题。

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

答案 7 :(得分:1)

这解决了我昨天在Tensorflow和Python 3.7.5中使用google.protobuf导入的问题。

检查protobuf在哪里

pip show protobuf

如果已安装,则会显示类似的内容

Name: protobuf
Version: 3.6.1
Summary: Protocol Buffers
Home-page: https://developers.google.com/protocol-buffers/
Author: None
Author-email: None
License: 3-Clause BSD License
Location: /usr/lib/python3/dist-packages
Requires: 
Required-by: tensorflow, tensorboard

(如果没有,请运行pip install protobuf

现在移到位置文件夹。

cd /usr/lib/python3/dist-packages

现在运行

touch google/__init__.py

答案 8 :(得分:0)

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

解决方案1:

pip uninstall protobuf
pip uninstall google
pip install google
pip install protobuf
pip install google-cloud

解决方案2:

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

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

希望它能工作。

答案 9 :(得分:0)

如果已安装protobuf,则可以这样导入

pip install protobuf

import google.protobuf

答案 10 :(得分:0)

I had this issue用于DGraph DB的Python包装器,该包装由this commit(可能有人使用)修复。

答案 11 :(得分:0)

要查找名称 google 冲突的地方.... 试试这个:

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<canvas id="myChart"></canvas>

然后python3

...我得到了关于 google-auth 的信息:

>>> help('google')

也试试

NAME
    google

PACKAGE CONTENTS
    auth (package)
    oauth2 (package)

然后

pip show google-auth

...然后重试sudo pip3 uninstall google-auth

然后我看到了protobuf:

>>> help('google')

答案 12 :(得分:-1)

如果您是Windows用户,并尝试在cmd中启动py-script,请不要忘记在文件名前键入 python

python script.py

如果忘记键入“我没有命名为Google的模块”错误。