python解决ImportError: No module named google.protobuf
关于protocol buffer的优点,就过多涉及;如果涉及到数据传输和解析,使用pb会比自己去写解析代码更有效率,至少对于大部分而言是这样的。一、下载,安装
到code.google.com下载源码,解压:
./configure && make && make check && make install
最后一步涉及到权限,可能会需要sudo。二、定义一个proto文件下面依然是给出一个简单的例子,要使用proto首先需要自己定义一个proto文件,定义一个people.proto文件,内容如下:
message people { optional string name = 1; optional int32 height = 2; }三、生成一个python可用的py文件
然后就是生成对应的py文件,命令如下:
protoc -I=./ --python_out=./ people.proto其中-I是source的路径,--python_out表示对应python库的生成路径,然后是对应的proto文件。当然,pb还支持c++和java,修改--python_out即可。完成后就有对应的people_pb2.py文件了。导入后即可使用,第一次安装后直接用应该会提示:ImportError: No module named google.protobuf,这是因为找不到对应的库路径导致,到你下载的pb路径下,找到python路径,执行sudo python setup.py install,执行完后可以通过执行sudo python setup.py test检查是否有安装成功,如果最后提示----------------------------------------------------------------------Ran 193 tests in 0.327sOK那么就是安装成功了,此时再导入对应的pb2.py文件即可使用。--------------------------------------------------------------------------------------------------------------------------------------------------------执行python setup.py install 时可能有:This script requires setuptools version 0.6c11 to run可从:http://download.csdn.net/download/fhqsse220/5602687下载gzip -d setuptools-0.6c11.tar.gztar xf setuptools-0.6c11.tarcd setuptools-0.6c11python setup.py install返回如下则表示安装成功Installed /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.eggProcessing dependencies for setuptools==0.6c11Finished processing dependencies for setuptools==0.6c11----------------------------------------------------------------------------------------------继续报错:error: package directory 'google/protobuf/compiler' does not exist解决办法:在当前目录下的google/protobuf/下创建compiler文件夹再次在protobuf文件夹下运行:python setup.py install 。最终提示安装成功。 -------------------------------------------------------------------------------------------------下面示例:
1 准备.proto文件
struct_oss_pb.proto
message entity_attr { required int32 attr_id = 1; // 属性类型标识,比如:标题属性为 1,正文属性为2,图片属性为 3,发现时间属性为4,原始url属性为5 ,父页面属性为 6; required bytes attribute = 2; // 属性类型描述,比如“标题”,“ 正文”,“图片”,“发现时间”,“原始 url”,“父页面 ”等 repeated bytes value = 3; // 属性值,除“图片”只保留 osskey之外,其他保留原文。考虑到文章中会保留多幅图,所以采用repeated。 }; message entity_desc { required int32 entity_id = 1; // 实体类型标识,比如:新闻为 1,小说为2 。 required bytes entity_name = 2; // 实体名称,比如:新闻主题事件关键词,小说名等。 repeated entity_attr attributes = 3; // 属性描述,格式见entity_attr。 };
2. 将proto转化为 xxx_pb2.py ,然后在你的程序里import这个py
protoc --python_out=./ ./struct_oss_pb.proto
得到struct_oss_pb_pb2.py
3. 读写protobuf的示例python
test_pb.py
01 # coding: gbk 02 import struct_oss_pb_pb2 03 entitydesc=struct_oss_pb_pb2.entity_desc() 04 entitydesc.entity_id=1 05 entitydesc.entity_name='haha' 06 07 #create proto 08 entityattr=entitydesc.attributes.add() #嵌套message 09 entityattr.attr_id = 11 10 entityattr.attribute = '标题'.decode('gbk').encode('utf-8') 11 entityattr.value.append("title adfadf") 12 13 entity_attr_str=entityattr.SerializeToString() 14 print entity_attr_str 15 entitydesc_str=entitydesc.SerializeToString() 16 print entitydesc_str 17 print '----' 18 #read 19 entityattr2 = struct_oss_pb_pb2.entity_attr() 20 entityattr2.ParseFromString(entity_attr_str) 21 print entityattr2.attr_id 22 print entityattr2.attribute.decode('utf-8').encode('gbk') 23 for i in entityattr2.value: 24 print i 25 26 print '----' 27 entitydesc2=struct_oss_pb_pb2.entity_desc() 28 entitydesc2.ParseFromString(entitydesc_str) 29 print entitydesc2.entity_id 30 #repeated entity_attr attributes,由于是repeated需要遍历 31 for oneatt in entitydesc2.attributes: 32 print oneatt.attr_id 33 for i in oneatt.value: 34 print i