Ask Your Question
0

sudo can't find rospy

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

twigs gravatar image

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

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: https://www.raspberrypi.org/forums/vi... ) 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 listener2.py sudo python listener2.py

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

Thanks!

edit retag flag offensive close merge delete

Comments

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

2 Answers

Sort by ยป oldest newest most voted
3

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

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).

edit flag offensive delete link more

Comments

1

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 -0500 )edit

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

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

answered 2015-11-17 17:11:03 -0500

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

Stats

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

Seen: 3,156 times

Last updated: Nov 18 '15