ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

sudo can't find rospy

asked 2015-11-17 14:57:34 -0600

twigs gravatar image

updated 2015-11-17 17:29:06 -0600

We are trying to run a publisher/subscriber on a raspberry pi.

The talker captures keyboard input and the listener takes the key presses and controls LEDs connected to the RasPi pins.

Everything seems to be working fine except for one annoying error.

The listener needs to be run using sudo so it can control the pins.(More info here: ) But when I run the listener using sudo, I receive the error "no module named rospy". Rospy works fine when I run the listener without root.

the two commands I'm running are: python sudo python

Is there something else I should be running instead of sudo python?


edit retag flag offensive close merge delete


gvdhoorn gravatar image gvdhoorn  ( 2015-11-18 02:25:34 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2015-11-18 05:57:22 -0600

The issue is almost certainly related to the fact that when you run sudo python you are running the "python" 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:

sudo su
source /opt/ros/indigo/setup.bash
rosrun <PACKAGE>

You could also work on editing your sudoers file to preserve relevant environment variables (e.g. PYTHONPATH).

edit flag offensive delete link more



Really, just don't run anything as root. In almost all cases it's just a matter of permissions, which can almost always be granted to individual users and / or groups. If you must use sudo (but really, don't, ever), invoking it as sudo -H .. is probably the easiest way.

gvdhoorn gravatar image gvdhoorn  ( 2015-11-18 08:15:47 -0600 )edit

@gvdhoorn is absolutely correct... avoid requiring sudo!

jarvisschultz gravatar image jarvisschultz  ( 2015-11-18 15:54:05 -0600 )edit

answered 2015-11-17 17:11:03 -0600

This is because with sudo, you're running commands in root's environment, and ROS isn't configured there. Rather than run nodes with sudo, you should add a udev rule which configures the GPIO device so non-root users can access it.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-11-17 14:57:34 -0600

Seen: 3,750 times

Last updated: Nov 18 '15