导入的python子模块无法导入同级子模块,未命名模块

时间:2020-07-25 01:49:38

标签: python python-3.x grpc-python

我 有一个具有以下代码/目录结构的python项目:

main.py
pb/
  pb_pb2_grpc.py
  pb_pb2.py
// main.py
from pb.pb_pb2_grpc import add_DesignOutputServiceServicer_to_server
//pb_pb2_grpc.py
import pb_pb2 as pb__pb2

运行main.py会给我以下错误:

python3.7 -m main
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/main.py", line 6, in <module>
    from pb.pb_pb2_grpc import add_DesignOutputServiceServicer_to_server
  File "/pb/pb_pb2_grpc.py", line 5, in <module>
    import pb_pb2 as pb__pb2
ModuleNotFoundError: No module named 'pb_pb2'

如何获取此子模块以正确导入其兄弟姐妹? pb_pb2_grpc.pypb_pb2.py都是生成的代码,无法修改。

0 个答案:

没有答案