How to setup ROS with Python 3

Omri Ben-Bassat
Feb 27 · 3 min read

1. Basic Setup

Traceback (most recent call last):
File "/catkin_ws/src/ros_python3_issues/src/", line 3, in <module>
import rospy
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/", line 47, in <module>
from std_msgs.msg import Header
File "/opt/ros/melodic/lib/python2.7/dist-packages/std_msgs/msg/", line 1, in <module>
from ._Bool import *
File "/opt/ros/melodic/lib/python2.7/dist-packages/std_msgs/msg/", line 5, in <module>
import genpy
File "/opt/ros/melodic/lib/python2.7/dist-packages/genpy/", line 34, in <module>
from . message import Message, SerializationError, DeserializationError, MessageException, struct_I
File "/opt/ros/melodic/lib/python2.7/dist-packages/genpy/", line 44, in <module>
import yaml
ModuleNotFoundError: No module named 'yaml'
sudo apt-get install python3-pip python3-yaml
sudo pip3 install rospkg catkin_pkg

2. cv_bridge Issue

Traceback (most recent call last):
File "/catkin_ws/src/ros_python3_issues/src/", line 23, in <module>
ros_image = bridge.cv2_to_imgmsg(numpy.asarray(empty_image), encoding="rgb8") # convert PIL image to ROS image
File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/", line 259, in cv2_to_imgmsg
if self.cvtype_to_name[self.encoding_to_cvtype2(encoding)] != cv_type:
File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/", line 91, in encoding_to_cvtype2
from cv_bridge.boost.cv_bridge_boost import getCvType
ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)
sudo apt-get install python-catkin-tools python3-dev python3-numpy
mkdir ~/catkin_build_ws && cd ~/catkin_build_ws
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/
catkin config --install
mkdir src
cd src
git clone -b melodic
cd ~/catkin_build_ws
catkin build cv_bridge
source install/setup.bash --extend

3. rostest Issue

... logging to /root/.ros/log/rostest-477a6ee2f64d-4170.log
[ROSUNIT] Outputting test results to /root/.ros/test_results/ros_python3_issues/rostest-test_test_issue_rosunit.xml
Traceback (most recent call last):
File "/catkin_ws/src/ros_python3_issues/test/", line 18, in <module>
rostest.rosrun('ros_python3_issues', 'issue_rosunit', TestROSUnitIssue)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rostest/", line 146, in rosrun
result = rosunit.create_xml_runner(package, test_name, result_file).run(suite)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rosunit/", line 275, in run
result.print_report(stream, time_taken, out_s, err_s)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rosunit/", line 202, in print_report
stream.write(ET.tostring(self.xml(time_taken, out, err).getroot(), encoding='utf-8', method='xml'))
TypeError: write() argument must be str, not bytes
[Testcase: testissue_rosunit] ... ok
* TESTS: 0
cd ~/catkin_ws/src
git clone
cd ..
catkin_make_isolated --install --pkg rosunit -DCMAKE_BUILD_TYPE=Release --install-space /opt/ros/melodic

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade