user45245
user45245

Reputation: 905

When I try to generate files for protobuf I get error ModuleNotFoundError

When I try to generate files with the command

python -m grpc_tools.protoc -I./ --python_out=. --grpc_python_out=. service.proto

I get error.

Traceback:
test_client.py:11: in <module>
    from tests.remote.grpc_wrapper.service_pb2_grpc import TestServiceServicer, add_TestServiceServicer_to_server, \
service_pb2_grpc.py:4: in <module>
    import service_pb2 as service__pb2
E   ModuleNotFoundError: No module named 'service_pb2'

How can I fix it? I truing reinstall protobuf but it don’t help me.

pip uninstall protobuf
pip install -U protobuf

P.S. I use conda, I truing use

conda install protobuf

but it don’t help me too.

enter image description here

Upvotes: 5

Views: 6906

Answers (1)

Ed Harrod
Ed Harrod

Reputation: 3619

There's talk of specifying this at the point of generation in the .proto file in this issue. As far as I know you have two options currently:

1) Change your line 4 to have . in front (this signifies a relative import):

from . import service_pb2 as service__pb2

2) Change the __init__.py file in the "grpc_wrapper" folder to include:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

Upvotes: 12

Related Questions