【发布时间】:2015-12-25 11:25:15
【问题描述】:
我不明白为什么我不能让 python 获取我系统上的 google 协议缓冲区模块。
使用google protocol buffers 2.6.1的源码包,我创建了protobuf egg。使用 easy_install 安装后,一切正常。但是,对于我们的项目,我不希望人们必须安装 egg。所以相反,我想我会让同事安装一个特定于我们的构建系统的包,其中包含解包形式的 python 模块,并让 python 通过指定 PYTHONPATH 来获取它。但似乎无论我将 PYTHONPATH 设置为什么,python 都看不到包。
解压后的模块内容:
pkg
└── google
├── __init__.py
├── __init__.pyc
└── protobuf
├── compiler
│ ├── __init__.py
│ └── plugin_pb2.py
├── descriptor_database.py
├── descriptor_pb2.py
├── descriptor_pool.py
├── descriptor.py
├── __init__.py
├── internal
│ ├── api_implementation.cc
│ ├── api_implementation_default_test.py
│ ├── ...
│ ├── wire_format.py
│ └── wire_format_test.py
├── message_factory.py
├── message.py
├── pyext
│ ├── cpp_message.py
│ ├── descriptor.cc
│ ├── ...
│ ├── repeated_scalar_container.h
│ └── scoped_pyobject_ptr.h
├── reflection.py
├── service.py
├── service_reflection.py
├── symbol_database.py <-----------------
├── text_encoding.py
├── text_format.py
无论我将 PYTHONPATH 设置为 pkg/、pkg/google 还是 pkg/google/protobuf,python 都无法获取 symbol_database.py。
我用
python -c "from google.protobuf import symbol_database as _symbol_database"
测试它是否有效。我正在运行 python 2.7。
为什么这不起作用?
【问题讨论】:
-
将 PYTHONPATH 设置为 pkg/ 就足够了...但是您需要正确设置它,即。带有完整路径。
-
我做到了。我不想用完整的路径来打扰你们。可以肯定的是:它需要指向包含 __init__.py 的目录,对吧?
标签: python python-2.7 protocol-buffers pythonpath