【问题标题】:No module named 'google' when using protobuf in python在 python 中使用 protobuf 时没有名为“google”的模块
【发布时间】:2022-02-11 19:59:44
【问题描述】:

我正在尝试在 MAC 上的 python 中使用 protobuf 编写一个简单的程序。但我收到“no module named 'google'”错误。我使用“brew install protobuf”安装了 protobuf。

以下是文件。

addressbook.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

test.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'

谁能帮我解决这个问题。

【问题讨论】:

标签: python protocol-buffers


【解决方案1】:

试试

pip install protobuf (or pip3 install...)

如果你甚至没有安装 pip,那么

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2016-07-11
    • 2023-03-25
    • 2019-02-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多