Rosanswers logo


After endless hours of trying to make my ROS instance work with Python 3 I got stuck and confused.
I started with installing ROS on Raspbian using repositories and ended up even re-compiling everything. The same result I reached on Debian Buster installed on VirtualBox. My understanding was that Noetic will use Python 3 - was I wrong?
Or I just missed something?

Appreciate any help.

Immediate issue

When attempting to run a Python node, ROS fails with “ImportError: No module named yaml”.

voidmain@rob01:~/catkin_ws $ rosrun ir_test simple_topic_publisher.py
Traceback (most recent call last):
  File "/home/voidmain/catkin_ws/src/ir_test/nodes/simple_topic_publisher.py", line 3, in <module>
    import rospy
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/__init__.py", line 47, in <module>
    from std_msgs.msg import Header
  File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/__init__.py", line 1, in <module>
    from ._Bool import *
  File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/_Bool.py", line 6, in <module>
    import genpy
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/__init__.py", line 34, in <module>
    from . message import Message, SerializationError, DeserializationError, MessageException, struct_I
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 48, in <module>
    import yaml
ImportError: No module named yaml

The check with pip3 verifies that the module is installed:

voidmain@rob01:~/catkin_ws $ pip3 install pyyaml
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pyyaml in /usr/lib/python3/dist-packages (3.13)

By goofing around and installing yaml for pip (Python 2) I solved the issue however...

voidmain@rob01:~/catkin_ws $ pip install pyyaml
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simp
Collecting pyyaml
  Downloading https://files.pythonhosted.org/packages/a0/a4/d63f2d7597e1a4b4d6c5b029991d3b824b5bd331af8d4ab1ed687d/PyYAML-5.4.1.tar.gz (175kB)
    100% |████████████████████████████████| 184kB 988kB/s
  Installing build dependencies ... done
Building wheels for collected packages: pyyaml
  Running setup.py bdist_wheel for pyyaml ... done
  Stored in directory: /home/voidmain/.cache/pip/wheels/2a/d4/92/cf299bdf4ca8126b46e913e29f76a4f17ca762c45028
Successfully built pyyaml
Installing collected packages: pyyaml
Successfully installed pyyaml-5.4.1

...unfortunately there are next issues like missing rospkg module. After that I get some other errors.

So, once again: why Python 2 is in the picture? How do I resolve it.


Raspberry Pi 3 B+

voidmain@rob01:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION="10 (buster)"


voidmain@rob01:~ $ env | grep ROS

ROS build command

$ ./src/catkin/bin/catkin_make_isolated --install-space /opt/ros/noetic --install -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/bin/python3


voidmain@rob01:~ $ which python
voidmain@rob01:~ $ which python3

Originally posted by void_main on ROS Answers with karma: 16 on 2021-03-22

Post score: 0


1 Answer 1


Rosanswers logo

It is embarrassing but just after posting this I came across the answer: https://answers.ros.org/question/361966/rosrun-still-use-python27-instead-of-38-inside-noetic/

I was missing '3' in the script's shebang:

#!/user/bin/env python3 

fixed everything.

Originally posted by void_main with karma: 16 on 2021-03-22

This answer was ACCEPTED on the original site

Post score: 0

Original comments

Comment by osilva on 2021-09-15:
Thank you for sharing. I am sure many of us has come up across this at some stage.

Comment by habib muhammad thariq on 2022-02-18:
This issue also happened to me. But i didn't have python 2.7, so pyyaml module still can't install on me. I also try the solution above, but i still got the same error. Is there anyone who know what should i do ?


Your Answer

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