Python3:尝试导入`google.protobuf`时“没有名为'<some.module>'的模块”的可能原因?

时间:2015-05-05 09:08:42

标签: python-3.x protocol-buffers python-import

我正在尝试为Python3构建Google协议缓冲区(protobuf)版本3.0.0-alpha2(也尝试过2.6.1),我遇到了一些困难。

在Ubuntu 14.04上使用Python 3.4.0和2.7.6我通过运行

从源代码构建protobuf
./autogen.sh
./configure --prefix=`pwd`/../build-3.0.0
cd python
python3 ./setup.py build --cpp_implementation
# run build-3.0.0/bin/protoc to create *_pb2.py files

我在Python2上做了同样的事情,有两个不同的结果:运行Python2

LD_LIBRARY_PATH=./build-3.0.0/lib \
PYTHONPATH=./protobuf-git/python:./protobuf-git/python/build/lib.linux-x86_64-2.7/ \
python -c "import google.protobuf"

..返回时没有错误。尝试使用Python3(当然还有改编的路径凭证)会返回:

LD_LIBRARY_PATH=./build-3.0.0/lib \
PYTHONPATH=./protobuf-git/python:./protobuf-git/python/build/lib.linux-x86_64-3.4/ \
python3 -c "import google.protobuf"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'google.protobuf'

然后我比较了python2.7和python3.4的两个生成文件夹,但除了一些不同的文件内容外,它们似乎是相同的。

所以我的问题: No module named 'google.protobuf'可能是什么原因?两种情况下都存在指定的文件夹,那么为什么Python2会找到google.protobuf而Python3却找不到?

这是protobuf-git

中文件树的摘录
    protobuf-git
    |-- ...
    |-- python
    |   |-- README.md
    |   |-- build
    |   |   |-- lib.linux-x86_64-2.7
    |   |   |   `-- google
    |   |   |       |-- __init__.py
    |   |   |       `-- protobuf
    |   |   |           |-- __init__.py
    |   |   |           |-- compiler
    |   |   |           |   |-- __init__.py
    |   |   |           |   `-- plugin_pb2.py
    |   |   |           |-- ...
    |   |   |-- lib.linux-x86_64-3.4
    |   |   |   `-- google
    |   |   |       |-- __init__.py
    |   |   |       `-- protobuf
    |   |   |           |-- __init__.py
    |   |   |           |-- compiler
    |   |   |           |   |-- __init__.py
    |   |   |           |   `-- plugin_pb2.py
    |   |   |           |-- ...
    |   |-- google
    |   |   |-- __init__.py
    |   |   `-- protobuf
    |   |       |-- __init__.py
    |   |       |-- compiler
    |   |       |   |-- __init__.py
    |   |       |   `-- plugin_pb2.py
    |   |       |-- ...
    |   |       |-- internal
    |   |       |   |-- __init__.py
    |   |       |   |-- ...

0 个答案:

没有答案