Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get an error when importing my auto-generated gRPC code in Python?

I have to write a RPC service for some Python apps. I have an IDL file called solver.proto and generated some code from it using the grpcio-tools module. Here is the resulting project structure:

solver-service
  ├── main.py
  └── protos/
       ├── __init__.py       
       ├── __pycache__
       │     └── ... 
       ├── generate_pb.py
       ├── solver.proto
       ├── solver_pb2.py
       └── solver_pb2_grpc.py

I have my PYTHONPATH set to /abs/path/to/solver-service and I try to import my auto-generated gRPC classes from this directory but hit the following:

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 "<stdin>", line 1, in <module>
  File "/abs/path/to/solver-service/protos/solver_pb2_grpc.py", line 4, in <module>
    import solver_pb2 as solver__pb2
ImportError: No module named 'solver_pb2'

So for whatever reason I can import protos.solver_pb2 successfully but the interpreter complains about importing protos.solver_pb2_grpc due to some relative path issue. Calling all Python devs doing gRPC work out there -- how do I get this working?

like image 461
nmurthy Avatar asked Feb 28 '26 18:02

nmurthy


2 Answers

I know this is old, but usually you have to add from . before the import in the _grpc.py file. solver_pb2_grpc imports solver_pb2, but for some reason the PYTHONPATH doesn't check the directory where the protos are exported in python.

like image 124
Eli Selkin Avatar answered Mar 03 '26 06:03

Eli Selkin


I had a similar issue couple of years back with a proto that imported another proto. Long story short, I spent about a week on this, tried all the public python proto generation libs I could find on Github but still couldn't solve the issue. The final solution was to fix the relative path of the import inline using sed as part of the build script. Trust me, anyone seeing this issue in the future, it's not worth spending time fixing it, just figure out the correct path and write a script to replace the imports in the generated files.

like image 39
farbo Avatar answered Mar 03 '26 06:03

farbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!