簡體   English   中英

在 python 中使用 protobuf 時沒有名為“google”的模塊

[英]No module named 'google' when using protobuf in python

我正在嘗試在 MAC 上使用 python 中的 protobuf 編寫一個簡單的程序。 但我收到“沒有名為‘google’的模塊”錯誤。 我使用“ brew install protobuf ”安裝了 protobuf。

以下是文件。

地址簿.proto:

syntax = "proto3";

package tutorial;

message Person {
  optional string name = 1;
  optional int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    optional string number = 1;
    optional PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}

我使用如下的 protoc 命令來生成 addressbook_pb2.py

protoc -I=. --python_out=. ./addressbook.proto --experimental_allow_proto3_optional

測試.py:

import addressbook_pb2
import sys

def writeData():
    return

if __name__ == "__main__":
    writeData()

我遇到錯誤。

Traceback (most recent call last):
  File "/Users/ka/Desktop/proto_python/test.py", line 1, in <module>
    import addressbook_pb2
  File "/Users/ka/Desktop/proto_python/addressbook_pb2.py", line 5, in <module>
    from google.protobuf import descriptor as _descriptor
ModuleNotFoundError: No module named 'google'

有人可以幫我解決這個問題嗎?

嘗試

pip install protobuf (or pip3 install...)

如果你甚至沒有安裝 pip 那么

sudo apt-get install python-pip  (or python3-pip)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM