【问题标题】:ImportError: No module named google.protobufImportError:没有名为 google.protobuf 的模块
【发布时间】:2016-12-05 10:57:12
【问题描述】:

我正在关注 this guide 并使用 addressbook.proto 的确切示例。

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

顺便说一句,在 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时的错误信息,

编辑 4

which pip 的输出,

which pip
/Users/foo/miniconda2/bin/pip

sys.executable 的输出,

/Users/foo/anaconda/bin/python

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

【问题讨论】:

  • 您是否将其中一个文件命名为“google.py”?
  • Lin Ma,你有两个独立的 python 安装正在运行。 /Users/foo/anaconda/ 上的一个(这是从您的代码中调用的那个)和 /Users/foo/miniconda2/` 中的一个(这是您从终端键入 pip 时调用的那个。考虑找到 scripts/Users/foo/anaconda 中的bin 目录(以包含pip 的为准)并从该目录中运行pip install ...。这应该将protobuf 包安装在正确的位置,以便您在当前代码设置中使用它。跨度>
  • @jedwards,很好地抓住并投票。想知道我应该使用pip install google 还是pip install protobuf
  • @jedwards,尝试使用完整路径执行pip,请参阅编辑 5 部分的更新。似乎据说protobuf已经在那里了。然后我尝试从 PyCharm 执行,遇到了同样的问题,No module named google.protobuf,如果您有任何进一步的想法,那就太好了。 :)
  • 尝试cd /Users/foo/anaconda/bin 然后./pip install protobuf -- 或 -- cd /Users/foo/anaconda/bin 然后./python -m pip install protobuf

标签: python python-2.7 protocol-buffers


【解决方案1】:

我正在使用 jetson nano 喷气背包 4.6 简单的方法:

让 tensorflow 卸载并重新安装

对我有用

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

安装 protobuf:

pip install protobuf

然后像这样导入它:

import google.protobuf

【讨论】:

    【解决方案3】:

    要查找名称 google 冲突的位置 .... 试试这个:

    python3
    

    然后&gt;&gt;&gt; help('google')

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

    NAME
        google
    
    PACKAGE CONTENTS
        auth (package)
        oauth2 (package)
    

    然后试试

    pip show google-auth
    

    然后

    sudo pip3 uninstall google-auth
    

    ...然后重试&gt;&gt;&gt; help('google')

    然后我看到了protobuf:

    NAME
        google
    
    PACKAGE CONTENTS
        protobuf (package)
    

    【讨论】:

      【解决方案4】:

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

      python script.py
      

      如果忘记输入,我会出现“没有名为 google 的模块”错误。

      【讨论】:

        【解决方案5】:

        造成这种情况的原因主要是 evil 命令pip install google。对于google-cloud,我遇到了类似的问题,但对于protobuf,同样的步骤也是如此。我们的两个问题都处理“google”命名空间上的命名空间冲突。

        如果您像我一样执行了pip install google 命令,那么您来对地方了。 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 &gt; cloud,这会导致这两个包的命名空间冲突。

        google-protobufgoogle &gt; protobuf下面的截图命名空间中查看

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

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

        此外,非官方的“google”软件包安装了“soupsieve”和“beautifulsoup4”。您可能还想卸载这些软件包。

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

        【讨论】:

        • @GrahamPHeath 发生了!即使对我来说也花了很多时间,因为这不是很明显的原因。但是干杯我很高兴它适用于你的情况:)
        • 这是一个巨大的安全问题,允许一些随机的家伙拥有一个名为 google 的包。
        【解决方案6】:

        这解决了我昨天在 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

        现在进入 location 文件夹。

        cd /usr/lib/python3/dist-packages
        

        现在运行

        touch google/__init__.py
        

        【讨论】:

        • 没有意识到 protobuf 有时会安装到与大多数其他软件包不同的位置。 /usr/local/lib64/python3.6/site-packages 就我而言。
        【解决方案7】:

        I had this issue 使用 DGraph DB 的 Python 包装器时,this commit 以某种方式修复了该问题(可能对某人有用)。

        【讨论】:

          【解决方案8】:

          我也有这个问题,已经研究了很长时间。在 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
          

          希望它会起作用。

          【讨论】:

          • 这对我有用,没有 pip install google-cloud 部分。
          【解决方案9】:

          我收到了与标题相同的错误消息,但在我的情况下,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 重新安装)解决了这个问题。

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

          【讨论】:

            【解决方案10】:

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

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

            pip install google
            

            【讨论】:

            【解决方案11】:

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

            【讨论】:

              【解决方案12】:

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

              conda install protobuf
              

              【讨论】:

              • 如果不使用conda,则必须这样做:sudo pip install googlesudo pip install protobuf
              • 我使用 anaconda。为什么我必须这样做这个包?为什么python -m pip install protobuf 不起作用?
              • 不适合我。我正在尝试 tensorflow 1.4 版。
              • 我用 pip3 试过了。 pip3 install protobuf 工作pip3 install protobuf3 无法显式安装google 包。
              【解决方案13】:

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

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

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

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

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

              要在这个virtualenv安装包,需要激活环境变量设置:

              . virtualenv/bin/activate
              

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

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

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

              【讨论】:

              • 谢谢 janos,投票,你知道 google.protobuf 是否适用于像 miniconda2 或 conda 这样的 python 解释器?如果不是,我需要更改 Python 解释器。想知道您使用的 Python 解释器是什么?
              • 我使用的是官方的 Python 3.5 或 Python 2.7 解释器。但我描述的方法也应该适用于其他人。只需按照我描述的步骤仔细操作即可。您所需要的只是确保您在命令行上使用pip 和PyCharm 使用相同的解释器。正如我解释的那样,确保这一点的最佳方法是在两者中使用相同的virtualenv
              【解决方案14】:

              你应该运行:

              pip install protobuf
              

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

              根据this link

              【讨论】:

              • 感谢大卫,投票并尝试但不为我工作。在编辑 1 部分中发布错误消息。如果您有任何进一步的想法,那就太好了。
              • 为什么这不起作用?为什么我必须改为conda install protobuf
              猜你喜欢
              • 1970-01-01
              • 2023-04-01
              • 2015-09-27
              • 1970-01-01
              • 2013-03-11
              • 2012-12-07
              • 2012-05-23
              • 2019-07-29
              • 2015-07-04
              相关资源
              最近更新 更多