【发布时间】:2022-06-30 22:04:17
【问题描述】:
我正在尝试使用 GRPC tutorial 使用 GRPC 我已经生成了两个文件:
helloworld_pb2_grpc.py
helloworld_pb2.py
我试图将这些文件放在一个名为 PB 的文件夹下,文件夹结构如下:
#root/pb
helloworld_pb2_grpc.py
helloworld_pb2.py
#root/GRPC
greeter_server.py
#root/Client
greeter_client.py
当我使用相同的根文件夹运行代码时,它工作正常。但是当我将这些文件放在文件夹下时,我无法导入生成的文件。我正在使用这段代码: #GRPC/greeter_server.py 导入系统
# adding Folder_2 to the system path
sys.path.insert(0, '/root/pb')
from helloworld_pb2_grpc import helloworld_pb2_grpc
from helloworld_pb2 import helloworld_pb2
当我运行我得到的代码时:
Traceback (most recent call last):
File "GRPC/greeter_server.py", line 25, in <module>
from helloworld_pb2_grpc import helloworld_pb2_grpc
ImportError: cannot import name 'helloworld_pb2_grpc' from 'helloworld_pb2_grpc' (/root/pb/helloworld_pb2_grpc.py)
你可以找到文件helloworld_pb2_grpc.py,helloworld_pb2.py的内容
【问题讨论】:
标签: python-3.x grpc grpc-python