我有一个Python项目,它使用setuptools进行部署,我主要遵循关于项目结构的this guide。该项目使用Google协议缓冲区来定义网络消息格式。我的主要问题是如何在安装期间使setup.py调用protoc编译器,以将定义构建到一个_pb2.py文件中。
在this question中,建议只将生成的pb2.py文件与项目一起分发。虽然这可能适用于非常相似的平台,但我发现有几个情况下这不起作用。例如,当我在一个使用Anaconda Python的Mac上进行开发,并将生成的pb2.py和项目的其余部分复制到一个运行Raspbian的Raspberry Pi时,总会有来自pb2.py模块的导入错误。但是,如果我在Pi上新编译.proto文件,项目将按预期工作。因此,分发编译后的文件似乎不是一个选项。
在这里寻找工作和最佳实践解决方案。可以假设protoc编译器安装在目标平台上。
编辑:
因为人们会问失败的原因。在Mac上,protobuf版本是2.6.1。圆周率是2.4.1。显然,生成的protoc编译器输出所使用的内部API已经改变。结果基本上是:
File "[...]network_manager.py", line 8, in <module>
import InstrumentControl.transports.serial_bridge_protocol_pb2 as protocol
File "[...]serial_bridge_protocol_pb2.py", line 9, in <module>
from google.protobuf import symbol_database as _symbol_database
ImportError: cannot import name symbol_database
好的,我解决了这个问题,不需要用户安装特定的旧版本,也不需要在我的开发机器之外的另一个平台上编译proto文件。它的灵感来自this setup.py script from protobuf itself。
首先,需要找到protoc,可以使用
此函数将编译一个.proto文件并将_pb2.py放在同一位置。但是,行为可以任意改变。
接下来,派生类build py和clean来添加building和cleaning协议缓冲区。
最后,参数
需要添加到对安装程序的调用中,一切都应该正常。仍然需要检查可能的怪癖,但到目前为止,它在Mac和Pi上工作得完美无瑕。
我刚刚开始使用protobuf setuptools包来使用这段代码中最理智的部分。它仍然需要改进,所以欢迎任何反馈!
检查一下:https://pypi.python.org/pypi/protobuf-setuptools
另一个解决方案是将protobuf库与应用程序捆绑在一起,而不是在目标计算机上使用已安装的版本。这样,您就知道没有版本与生成的代码不匹配。
相关问题 更多 >
编程相关推荐