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

夙愿已清 提交于 2020-07-22 22:08:19

问题


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.


回答1:


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__)))


来源:https://stackoverflow.com/questions/53934591/when-i-try-to-generate-files-for-protobuf-i-get-error-modulenotfounderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!