0
$\begingroup$

Rosanswers logo

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():
    rospy.init_node("arm_gui")

main()

And the full error text:

rosrun SCARA_test_1 random.py
Traceback (most recent call last):
  File "/home/mint/Documents/catkin_ws/src/SCARA_test_1/scripts/random.py", line 3, in <module>
    import rospy
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/__init__.py", line 49, in <module>
    from .client import spin, myargv, init_node, \
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/client.py", line 46, in <module>
    import random
  File "/home/mint/Documents/catkin_ws/src/SCARA_test_1/scripts/random.py", line 34, in <module>
    main()
  File "/home/mint/Documents/catkin_ws/src/SCARA_test_1/scripts/random.py", line 30, in main
    rospy.init_node("arm_gui")
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


Originally posted by Vasu Agrawal on ROS Answers with karma: 13 on 2015-06-29

Post score: 0


Original comments

Comment by mgruhler on 2015-06-30:
Can you please give some more details about your setup? What OS/ROS Distro do you have? What does your PYTHONPATH look like?

Comment by Vasu Agrawal on 2015-06-30:
Ah, sorry. Edited post with additional info.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by ahendrix with karma: 47576 on 2015-06-30

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Vasu Agrawal on 2015-06-30:
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!

Comment by ahendrix on 2015-06-30:
.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.

Comment by mgruhler on 2015-07-01:
great catch, would never have tought of this ;-)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.