作者:晓辉19890424俱乐部 | 来源:互联网 | 2023-01-20 13:20
我必须为某些Python应用程序编写RPC服务。我有一个IDL文件solver.proto
,并使用该grpcio-tools
模块从中生成了一些代码。这是结果项目结构:
solver-service
??? main.py
??? protos/
??? __init__.py
??? __pycache__
? ??? ...
??? generate_pb.py
??? solver.proto
??? solver_pb2.py
??? solver_pb2_grpc.py
我将PYTHONPATH设置为/abs/path/to/solver-service
,我尝试从该目录导入自动生成的gRPC类,但是单击以下命令:
solver-service$ python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import protos.solver_pb2
>>> import protos.solver_pb2_grpc
Traceback (most recent call last):
File "", line 1, in
File "/abs/path/to/solver-service/protos/solver_pb2_grpc.py", line 4, in
import solver_pb2 as solver__pb2
ImportError: No module named 'solver_pb2'
因此,无论出于何种原因,我都可以protos.solver_pb2
成功导入,但是protos.solver_pb2_grpc
由于某些相对路径问题,解释器抱怨导入。召集所有进行gRPC的Python开发人员进行工作-如何使它工作?