mvukov / rules_ros

Build ROS (1) with Bazel
Apache License 2.0
21 stars 11 forks source link

ModuleNotFoundError: No module named 'rospy' when launched via launch file #35

Open JohnRudolfLewis opened 1 month ago

JohnRudolfLewis commented 1 month ago

I am unable to launch a python node via a launch file and ros_launch rule.

It has trouble finding the message and rospy when launched this way. I can fix the message by further defining it (import examples.chatter.example_msgs.msg) but am unable to get it to find rospy.

Please add an example of launching a python node via launch file. Perhaps make the existing example launch both listeners?

JohnRudolfLewis commented 1 month ago

Re-created problem here: https://github.com/JohnRudolfLewis/bazel-ros-example

ModuleNotFoundError: No module named 'rospy'

$ bazel run //chatter:chatter
INFO: Analyzed target //chatter:chatter (1 packages loaded, 16 targets configured).
INFO: Found 1 target...
Target //chatter:chatter up-to-date:
  bazel-bin/chatter/chatter
  bazel-bin/chatter/chatter_launch.py
INFO: Elapsed time: 0.945s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/chatter/chatter
... logging to /home/vscode/.ros/log/a64df9b8-4eb5-11ef-b119-bf9446a06087/roslaunch-edfa49feca88-51089.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://edfa49feca88:37901/

SUMMARY
========

NODES
  /
    listener (chatter/listener)
    listenerpy (chatter/listener.py)
    talker (chatter/talker)

auto-starting new master
process[master]: started with pid [51800]
ROS_MASTER_URI=http://localhost:11311/

setting /run_id to a64df9b8-4eb5-11ef-b119-bf9446a06087
process[rosout-1]: started with pid [51811]
started core service [/rosout]
process[talker-2]: started with pid [51816]
process[listener-3]: started with pid [51817]
process[listenerpy-4]: started with pid [51818]
Traceback (most recent call last):
  File "/home/vscode/.cache/bazel/_bazel_vscode/0cd11761e4a882ffd46b0cb0705acf1d/execroot/_main/bazel-out/k8-fastbuild/bin/chatter/chatter.runfiles/_main/chatter/listener.py", line 6, in <module>
    import rospy
ModuleNotFoundError: No module named 'rospy'
[listenerpy-4] process has died [pid 51818, exit code 1, cmd chatter/listener.py __name:=listenerpy __log:=/home/vscode/.ros/log/a64df9b8-4eb5-11ef-b119-bf9446a06087/listenerpy-4.log].
log file: /home/vscode/.ros/log/a64df9b8-4eb5-11ef-b119-bf9446a06087/listenerpy-4*.log
JohnRudolfLewis commented 1 month ago

In order to launch a python node from a launch file I had to add a shebang to the top of the python file and mark it as executable. The only shebang that I got working so far was #!/usr/bin/env python3. My current suspicion is this is part of the issue.

FYI: If I remove the shebang like the examples in this repo suggest, I get the following error:

RLException: Unable to launch [listenerpy-4]. 
If it is a script, you may be missing a '#!' declaration at the top.
The traceback for the exception was written to the log file
JohnRudolfLewis commented 1 month ago

Adding @ros_comm//:rospy to third_party/ros/roslaunch/BUILD.bazel fixes the 'No module named 'rospy' error.

diff --git a/third_party/ros/roslaunch/BUILD.bazel b/third_party/ros/roslaunch/BUILD.bazel
index 06e19d2..09cdf2d 100644
--- a/third_party/ros/roslaunch/BUILD.bazel
+++ b/third_party/ros/roslaunch/BUILD.bazel
@@ -45,7 +45,6 @@ py_library(
         "//third_party/ros:roslib",
         "@ros_comm//:rosmaster",
         "@ros_comm//:rosparam",
-        "@ros_comm//:rospy",
         "@ros_comm_msgs//:py_rosgraph_msgs",
         "@ros_ros//:rosclean",
         requirement("pyyaml"),

But then if you are importing other types in your python script, message definitions for example, those imports will not work right. Still more work to do.

JohnRudolfLewis commented 1 month ago

needed to adjust the way that the message def was being imported such that it worked when being launched both directly and via the launch file. Submitted PR.