【问题标题】:"ImportError: No module named google.protobuf" but it's definitely installed“ImportError:没有名为 google.protobuf 的模块”,但它肯定已安装
【发布时间】:2016-09-19 22:10:41
【问题描述】:

我已经安装了protobuf 包,但我无法导入它。

> pip uninstall protobuf
... uninstalls
> pip install protobuf
... installs. confirm that it's installed:
pip install protobuf
Requirement already satisfied (use --upgrade to upgrade): protobuf in     ./.venv/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./.venv/lib/python3.5/site-packages (from protobuf)
Requirement already satisfied (use --upgrade to upgrade): six>=1.9 in ./.venv/lib/python3.5/site-packages (from protobuf)

回到ipython:

In [1]: from google.protobuf import descriptor as _descriptor
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-3baca6afb060> in <module>()
----> 1 from google.protobuf import descriptor as _descriptor

ImportError: No module named google.protobuf

In [2]:

我真的很难过。

> python --version
> Python 3.5.1

> which pip
/Users/skyler/work/TemplateProcessor/.venv/bin/pip
> pip --version
pip 8.1.2 from /Users/skyler/work/TemplateProcessor/.venv/lib/python3.5/site-packages (python 3.5)

【问题讨论】:

  • 你从活动的 venv 启动 ipython?
  • 我不是。但是本地安装ipython后,在venv中通过pip,出现同样的错误。
  • 阅读此question,希望对您有所帮助。

标签: python protocol-buffers python-3.5


【解决方案1】:

我遇到了同样的问题,并找到了this issue 中建议的解决方法:

1.添加新文件tensorflow/google/init.py:

try:
    import pkg_resources
    pkg_resources.declare_namespace(__name__)
except ImportError:
    import pkgutil
    __path__ = pkgutil.extend_path(__path__, __name__)

    Add namespace_packages to tensorflow/google/protobuf/python/setup.py:

setup(
    name='protobuf',
    namespace_packages=['google'],
    ...
)
  1. 重新编译/pip 安装。祝你好运!

【讨论】: