当前位置: 首页 > 工具软件 > Python grpc > 使用案例 >

Python gRPC 安装(modules/canbus/proto/chassis.proto: File not found.)

朱俭
2023-12-01

1. 安装依赖库

sudo pip3 install grpcio
sudo pip3 install protobuf
sudo pip3 install grpcio_tools

2. 生成对应文件

python -m grpc_tools.protoc -I=path_1 --python_out=path_2 --grpc_python_out=path_3 path_1.XXX.proto

其中:

  • path_1 为 proto 文件所在目录
  • path_2 为 .py 文件输出目录
  • path_3 为 grpc.py 文件输出目录

实际使用

python -m grpc_tools.protoc -I=./protos --python_out=./rpc_pkg --grpc_python_out=./rpc_pkg ./protos/facegroup.proto

3. 问题分析

遇到的问题

[wohu@in-cyber-docker:/apollo]$ python -m grpc_tools.protoc -I=./modules/control/proto -I=./modules/canbus/proto -I=./modules/common/proto --python_out=./cyber/python/cyber_ros_bridge/pb --grpc_python_out=./cyber/python/cyber_ros_bridge/pb  ./modules/control/proto/control_cmd.proto
modules/canbus/proto/chassis.proto: File not found.
modules/common/proto/drive_state.proto: File not found.
modules/common/proto/header.proto: File not found.
modules/common/proto/pnc_point.proto: File not found.
modules/common/proto/vehicle_signal.proto: File not found.
modules/control/proto/input_debug.proto: File not found.
modules/control/proto/pad_msg.proto: File not found.
control_cmd.proto:4:1: Import "modules/canbus/proto/chassis.proto" was not found or had errors.
control_cmd.proto:5:1: Import "modules/common/proto/drive_state.proto" was not found or had errors.
control_cmd.proto:6:1: Import "modules/common/proto/header.proto" was not found or had errors.
control_cmd.proto:7:1: Import "modules/common/proto/pnc_point.proto" was not found or had errors.
control_cmd.proto:8:1: Import "modules/common/proto/vehicle_signal.proto" was not found or had errors.
control_cmd.proto:9:1: Import "modules/control/proto/input_debug.proto" was not found or had errors.
control_cmd.proto:10:1: Import "modules/control/proto/pad_msg.proto" was not found or had errors.
....
control_cmd.proto:248:12: "InputDebug" is not defined.

当前目录下的文件为

[wohu@in-cyber-docker:/apollo]$ ls
BUILD                  CONTRIBUTING.md  MANIFESTO.md  WORKSPACE       bazel-apollo  bazel-testlogs  data    env.json   readthedocs.yml  tools
CODE_OF_CONDUCT.md     CPPLINT.cfg      README.md     apollo.doxygen  bazel-bin     collect_pre.sh  docker  modules    scripts          tox.ini
CODE_OF_CONDUCT_cn.md  LICENSE          RELEASE.md    apollo.sh       bazel-out     cyber           docs    nohup.out  third_party
[wohu@in-cyber-docker:/apollo]$ 

解决方法为:

python -m grpc_tools.protoc -I=./ --python_out=./cyber/python/cyber_ros_bridge/pb --grpc_python_out=./cyber/python/cyber_ros_bridge/pb  ./modules/control/proto/control_cmd.proto

其中的 -I=./ 表示当前目录,也就是说 -I 的目录必须和 control_cmd.proto 文件中的 import 语句组成的路径是可以找到其它 proto 文件的。

这样会在当前目录下生成一个 cyber/python/cyber_ros_bridge/pb 目录,其中有生成的 py 文件。

或者直接用下面命令

python -m grpc_tools.protoc -I=./ --python_out=./ --grpc_python_out=./  ./modules/control/proto/control_cmd.proto

会在当前目录下生成所有的 py 文件。

示例:

root@08265a65540a:/apollo# ls
BUILD            CPPLINT.cfg  README.md   WORKSPACE     apollo.doxygen  apollo_docker.sh  bazel-bin       bazel-out       cyber  docker  modules   readthedocs.yml  third_party
CONTRIBUTING.md  LICENSE      RELEASE.md  WORKSPACE.in  apollo.sh       bazel-apollo      bazel-genfiles  bazel-testlogs  data   docs    py_proto  scripts          tools
root@08265a65540a:/apollo# 

root@08265a65540a:/apollo# python -m grpc_tools.protoc -I=./  --python_out=./ --grpc_python_out=./  ./cyber/carla_bridge/carla_proto/proto/*.proto
root@08265a65540a:/apollo# ls
BUILD            CPPLINT.cfg  README.md   WORKSPACE     apollo.doxygen  apollo_docker.sh  bazel-bin       bazel-out       cyber  docker  modules   readthedocs.yml  third_party
CONTRIBUTING.md  LICENSE      RELEASE.md  WORKSPACE.in  apollo.sh       bazel-apollo      bazel-genfiles  bazel-testlogs  data   docs    py_proto  scripts          tools
root@08265a65540a:/apollo# ls cyber/carla_bridge/carla_proto/proto/
BUILD                           carla_camera_info.proto        carla_destroy_object_pb2.py       carla_get_blueprints_pb2_grpc.py  carla_spawn_object.proto         carla_walker_control_pb2.py
carla_actor.proto               carla_camera_info_pb2.py       carla_destroy_object_pb2_grpc.py  carla_marker.proto                carla_spawn_object_pb2.py        carla_walker_control_pb2_grpc.py
carla_actor_pb2.py              carla_camera_info_pb2_grpc.py  carla_ego_vehicle.proto           carla_marker_pb2.py               carla_spawn_object_pb2_grpc.py   carla_weather_parameters.proto
carla_actor_pb2_grpc.py         carla_clock.proto              carla_ego_vehicle_pb2.py          carla_marker_pb2_grpc.py          carla_status.proto               carla_weather_parameters_pb2.py
carla_basic_type.proto          carla_clock_pb2.py             carla_ego_vehicle_pb2_grpc.py     carla_object.proto                carla_status_pb2.py              carla_weather_parameters_pb2_grpc.py
carla_basic_type_pb2.py         carla_clock_pb2_grpc.py        carla_geometry.proto              carla_object_pb2.py               carla_status_pb2_grpc.py         carla_world_info.proto
carla_basic_type_pb2_grpc.py    carla_control.proto            carla_geometry_pb2.py             carla_object_pb2_grpc.py          carla_traffic_light.proto        carla_world_info_pb2.py
carla_bounding_box.proto        carla_control_pb2.py           carla_geometry_pb2_grpc.py        carla_odometry.proto              carla_traffic_light_pb2.py       carla_world_info_pb2_grpc.py
carla_bounding_box_pb2.py       carla_control_pb2_grpc.py      carla_get_blueprints.proto        carla_odometry_pb2.py             carla_traffic_light_pb2_grpc.py  cyber
carla_bounding_box_pb2_grpc.py  carla_destroy_object.proto     carla_get_blueprints_pb2.py       carla_odometry_pb2_grpc.py        carla_walker_control.proto
root@08265a65540a:/apollo# 

 类似资料: