无法从 grpc_tools 导入 _protoc_compiler

问题描述

我在使用 .proto 文件中的 grpc_tools生成代码时遇到问题:

Traceback (most recent call last):
  File "somepath\run_codegen.py",line 16,in <module>
    generate()
  File "somepath\run_codegen.py",line 6,in generate
    from grpc_tools import protoc
  File "C:\python39\lib\site-packages\grpc_tools\protoc.py",line 22,in <module>
    from grpc_tools import _protoc_compiler

我已经从 requirements.txt 安装了这些依赖项:

Flask~=1.1
grpcio
grpcio-tools
protobuf

enter image description here

名为 run_codegen 的脚本文件代码如下:

from grpc_tools import protoc

def generate():
    protoc.main((
        '','-I./FinancialDataScraper/protos','--python_out=./FinancialDataScraper/Generated','--grpc_python_out=./FinancialDataScraper/Generated','./FinancialDataScraper/protos/quotes.proto'
    ))

generate()

我不知道是否应该明确使用 grpciogrpcio_tools 包的版本号,或者这与 python 本身有关,或者我错过了一些其他依赖项安装。

解决方法

事实上,问题是因为我在我的环境之外执行了脚本,而环境没有被激活。