ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

rospy module init_node fails?

asked 2015-06-29 21:14:02 -0500

Vasu Agrawal gravatar image

updated 2015-06-30 11:10:52 -0500

I'm attempting to write a simple python node, but I can't even seem to get rospy imported into my script. Even just the import statement (after running chmod +x [filename]) fails. I'm unable to run the tutorial python nodes as well. What are the possible reasons why this could be happening? I've been able to get a node running just fine on C++.

My (snipped) code, for reference:

#!/usr/bin/env python
import rospy

def main():


And the full error text:

rosrun SCARA_test_1
Traceback (most recent call last):
  File "/home/mint/Documents/catkin_ws/src/SCARA_test_1/scripts/", line 3, in <module>
    import rospy
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/", line 49, in <module>
    from .client import spin, myargv, init_node, \
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/", line 46, in <module>
    import random
  File "/home/mint/Documents/catkin_ws/src/SCARA_test_1/scripts/", line 34, in <module>
  File "/home/mint/Documents/catkin_ws/src/SCARA_test_1/scripts/", line 30, in main
AttributeError: 'module' object has no attribute 'init_node'

EDIT: Additional system info:

OS: Linux Mint 17.1 (Based on ubuntu 14.04)

ROS: Indigo

PYTHONPATH: /home/mint/Documents/catkin_ws/devel/lib/python2.7/dist-packages:/opt/ros/indigo/lib/python2.7/dist-packages

edit retag flag offensive close merge delete


Can you please give some more details about your setup? What OS/ROS Distro do you have? What does your PYTHONPATH look like?

mgruhler gravatar image mgruhler  ( 2015-06-30 00:53:45 -0500 )edit

Ah, sorry. Edited post with additional info.

Vasu Agrawal gravatar image Vasu Agrawal  ( 2015-06-30 11:10:24 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-06-30 13:02:24 -0500

ahendrix gravatar image

You've named your script random, which overlaps with the system python module of the same name.

When rospy attempts to import random, it instead imports you script, and causes your code to run before the definition of rospy is complete.

You should name your script something else that doesn't overlap with the system python modules.

edit flag offensive delete link more


This seemed to be part of the issue. I was having some trouble after I renamed the file to something else, only to discover that running the other file for some reason ran the .pyc instead (I'd love to know why). However, deleting the .pyc and changing the file name works. Thank you!

Vasu Agrawal gravatar image Vasu Agrawal  ( 2015-06-30 19:40:51 -0500 )edit

.pyc files are compiled python byte code; python will treat them the same as their matching .py files, even if those .py files don't exist.

ahendrix gravatar image ahendrix  ( 2015-06-30 20:57:11 -0500 )edit

great catch, would never have tought of this ;-)

mgruhler gravatar image mgruhler  ( 2015-07-01 00:59:15 -0500 )edit

Question Tools


Asked: 2015-06-29 21:14:02 -0500

Seen: 1,457 times

Last updated: Jun 30 '15