python protobufs - 避免安装步骤?
我正在编写一个小型 python 实用程序,它将由中等非技术用户使用,并且需要与一些 protobuf 进行交互。
理想情况下,我希望在本地计算机上使用它的唯一先决条件是:
安装了python
* have an SVN checkout of the repository
* run a simple bash script to build the local proto .py definitions
* run "python myutility"
我在导入descriptor_pb2.py方面遇到了麻烦。 我见过为什么在使用 Google Protocol Buffers 时会看到“无法导入名称描述符_pb2”错误?, 但希望避免添加运行 proto SDK 安装程序的额外先决条件。 我修改了 bash 脚本以在本地层次结构中生成描述符_pb2.py, 它适用于从我的其他 _pb2.py 文件导入的第一级,但看起来描述符_pb2.py 本身尝试导入描述符_pb2 找不到它:
$ python myutility.py
Traceback (most recent call last):
File "myutility.py", line 4, in <module>
import protos.myProto_pb2
File "/myPath/protos/myProto_pb2.py", line 8, in <module>
from google.protobuf import descriptor_pb2
File "/myPath/google/protobuf/descriptor_pb2.py", line 8, in <module>
from google.protobuf import descriptor_pb2
ImportError: cannot import name descriptor_pb2
我的本地文件夹看起来像:
* myutility.py
* google/
* protobuf/
* descriptor.py
* descriptor_pb2.py
* protos
* myProto_ob2.py
另外,我是 python n00b,所以我可能忽略了一些显而易见的事情。
蒂亚, 猎户座
i'm writing a small python utility which will be consumed by moderately non-technical users and which needs to interface w/ some protobufs.
ideally, i would like the only prerequisites to using this on a local machine to be:
have python installed
* have an SVN checkout of the repository
* run a simple bash script to build the local proto .py definitions
* run "python myutility"
i'm running into trouble around importing descriptor_pb2.py, tho.
i've seen Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers? ,
but would like to avoid adding the additional prerequisite of having run the proto SDK installer.
i've modified the bash script to also generate descriptor_pb2.py in the local heirarchy,
which works for the first level of imports from my other _pb2.py files, but it looks like descriptor_pb2.py itself tries to import descriptor_pb2 can't find it:
$ python myutility.py
Traceback (most recent call last):
File "myutility.py", line 4, in <module>
import protos.myProto_pb2
File "/myPath/protos/myProto_pb2.py", line 8, in <module>
from google.protobuf import descriptor_pb2
File "/myPath/google/protobuf/descriptor_pb2.py", line 8, in <module>
from google.protobuf import descriptor_pb2
ImportError: cannot import name descriptor_pb2
my local folder looks like:
* myutility.py
* google/
* protobuf/
* descriptor.py
* descriptor_pb2.py
* protos
* myProto_ob2.py
also, i'm a python n00b, so it's possible i'm overlooking something obvious.
tia,
orion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文件 __ init __.py 是否存在于 google/protobuf 目录中?
Does the file __ init __.py exist in google/protobuf directory?
在描述符_pb2.py 中注释掉描述符_pb2.py 的导入本身解决了我的问题。感谢蒂姆·麦克拉伦的建议!
commenting out the import of descriptor_pb2.py in descriptor_pb2.py itself fixed my issue. thanks to tim mcclarren for suggesting that !