首先下載下傳:protobuf-2.5.0.tar.gz 和protoc-2.5.0-win32.zip。兩者的版本要對應;
将下載下傳的google protobuf解壓,會看到一個python目錄,Windows下可将vsprojects\Debug下的protoc.exe拷貝到python目錄下。
在cmd下,切換到該目錄,執行python setup.py install
注意了,這裡如果不導入一個C++編譯過的包,會出現這樣的錯誤
file google\protobuf\descriptor_pb2.py (for module google.protobuf.descriptor_pb2) not found
package init file 'google\protobuf\compiler\__init__.py' not found (or not a regular file)
檢視該目錄下的Reademe.txt,才知道python需要用到C++編譯後的某些接口,于是乎,用vs2010将protobuf
項目打開并完整讓其生成成功,然後将vsprojects\google\protobuf下的compiler複制到python\google\protobuf下。
ok,Processing dependencies for protobuf==2.5.0
Finished processing dependencies for protobuf==2.5.0
執行成功
以後需要用到google protobuf ,隻需将python下的google拷貝到相應目錄下(這個步驟也可以用添加到python庫路徑解決),
在py檔案寫入 import google.protobuf即可
當然也别忘了要improt .proto生成的檔案