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

Why is the "import" command not being recognized? [closed]

asked 2021-02-11 12:14:23 -0600

anonymous user

Anonymous

I'm using ROS noetic in a ubuntu 20.04 machine but "import" command is not working when i write a code in python. First, i run the code using the python3 interpreter (python3 filename.py) and it works, but when i use rosrun the "import" command is not recognized and the console prints this error

/home/krisv/Documentos/ros_ws/src/krisv_first_package/scripts/talker.py: línea 4: import: orden no encontrada
/home/krisv/Documentos/ros_ws/src/krisv_first_package/scripts/talker.py: línea 5: import: orden no encontrada
from: can't read /var/mail/std_msgs.msg
/home/krisv/Documentos/ros_ws/src/krisv_first_package/scripts/talker.py: línea 9: error sintáctico cerca del elemento inesperado `('
/home/krisv/Documentos/ros_ws/src/krisv_first_package/scripts/talker.py: línea 9: `def talker():'
edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by jarvisschultz
close date 2021-02-12 10:59:12.554058

1 Answer

Sort by » oldest newest most voted
0

answered 2021-02-11 15:09:24 -0600

Looks to me like the same problem as described in this answer. You likely have an improperly formatted or missing hashbang.

edit flag offensive delete link more

Comments

1

I would suggest to close this question as a duplicate.

It's indeed a missing or incorrect hashbang.

gvdhoorn gravatar image gvdhoorn  ( 2021-02-12 00:16:10 -0600 )edit

I already have the hashbang #!/usr/bin/env python in my code, and it still doesn't work. As I said before, the problem is not the code since when I execute it by calling the python interpreter directly it works(python3 filename.py) , but it does not work when I execute the node with rosrun (rosrun package_name filename.py) and the error "Import: command not found" arise

anonymous userAnonymous ( 2021-02-12 08:51:24 -0600 )edit

I already solved the problem. instead of putting #!/usr/bin/env python, i put #!/usr/bin/python3 and that solved my problem.

anonymous userAnonymous ( 2021-02-12 09:13:21 -0600 )edit

To be clear the purpose of the #! is to invoke the Python interpreter. If it works with python3 filename.py but not ./filename.py then that is almost certainly something wrong with your #! line. The error you posted is consistent with with a missing #!. The change from #!/usr/bin/env python to #!/usr/bin/python3 would most likely have solved an error like the following:

/usr/bin/env: 'python': No such file or directory

You might also want to be aware of the python-is-python3package on Ubuntu 20.04: https://askubuntu.com/questions/12967...

jarvisschultz gravatar image jarvisschultz  ( 2021-02-12 10:58:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2021-02-11 12:14:23 -0600

Seen: 3,670 times

Last updated: Feb 11 '21