ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
The issue is almost certainly related to the fact that when you run sudo python listener2.py
you are running the "python listener2.py" as a different user. When you source
the setup.bash
files that are either in your base install, or in a workspace, you are setting up a bunch of environment variables. One of the environment variables that you are modifying is the PYTHONPATH
that Python uses to find modules to import. You can see this by opening a new terminal that has not "sourced" any setup.bash
files, and run echo $PYTHONPATH
. Then run something like source /opt/ros/indigo/setup.bash
or source ~/catkin_ws/setup.bash
and re-run echo $PYTHONPATH
.
If you must run your node using sudo (my guess is there is a way around this... see @DanLazewatsky's answer), then you need to make sure that your environment for the root user is setup correctly. A simple way to do this would be a script that "wraps" the command you are actually trying to run. E.g. see the following bash script:
#!/bin/bash
sudo su
source /opt/ros/indigo/setup.bash
rosrun <PACKAGE> listener2.py
You could also work on editing your sudoers file to preserve relevant environment variables (e.g. PYTHONPATH).