Reputation: 2311
I am trying to use gRPC in Python. I have the following directory layout:
Oracle
|
│ setup.py
│
└───oracle
│ oracle_server.py
│ __init__.py
│
├───generated
│ oracle_pb2.py
│ oracle_pb2_grpc.py
│ __init__.py
│
│
└───protos
oracle.proto
where I ran
python -m grpc_tools.protoc -I ../protos --python_out=. --grpc_python_out=. ../protos/oracle.proto
in Oracle/generated
.
Problem:
Oracle/oracle/generated/oracle_pb2_grpc.py
starts with
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import oracle_pb2 as oracle__pb2
and that second import fails with ModuleNotFoundError: No module named 'oracle_pb2'
when I import oracle_pb2_grpc.py
from anywhere outside Oracle/oracle/generated
, for example with a
from oracle.generated import oracle_pb2_grpc
in
Oracle/oracle/oracle_server.py
.
Question:
Am I doing something wrong? Can I add an argument to the call of grpc_tools.protoc
such that it generates imports that work in my setup?
(The example on https://github.com/grpc/grpc/tree/master/examples/python/helloworld doesn't run into this problem because it has all files in the same directory. That's not flexible/clean enough for me)
Upvotes: 3
Views: 1961
Reputation: 81
grpc is generating python2 imports, use
2to3 -n -w *
in the output directory to change to python3
Upvotes: 0
Reputation: 1
I suspect that treating the 'generated' folder as a package is leading to broken imports. I recommend adding the scripts path using sys.path.append().
Upvotes: 0