The Robot Operating System (ROS) does not currently work out-of-the-box with Python 3. ROS officially supports Python 2.7 and ROS 2 supports Python 3 natively. Since Python 2.7 support is now deprecated, and most robots in 2020 still use ROS (not ROS 2), it becomes neccessary to set up Python 3 with ROS in order to take full advantage of the latest features in many useful libraries/toolboxes which require Python 3 to run. Setting up virtual environments (for example conda envs) for Python projects is also recommended practice, however getting Anaconda to work with ROS can be tricky.
This brief tutorial offers a step by step guide for running ROS with Python 3 and OpenCV from within a conda environment. It’s worthwhile noting that I use ROS Kinetic which is default complied with Python 2.7 instead of reinstalling and compiling ROS with Python 3. Assuming that a Python 3 conda environment has already been set up and activated (see this tutorial for details of how to do so) proceed with the steps below.
Install the ROS Python packages
- Install rospkg, catkin_pkg, and opencv:
conda install -c conda-forge rospkg catkin_pkg opencv
- Install rosbag and sensor msgs:
conda install -c conda-forge ros-rosbag ros-sensor-msgs
- Install opencv-python:
pip install opencv-python
- Disbale ROS Opencv (this is a hack since ROS OpenCV supports Python 2.7, so we rename the cv2.so library file to avoid conflicts so that import cv2 works):
sudo mv cv2.so cv2_ros.so
Install cv_bridge from source
cv_bridge, which is part of the ROS perception vision_opencv package, must be compiled with Python 3 enabled. Begin by creating and configuring a catkin workspace pointing to your Anaconda Python 3.
- Create the ROS workspace:
- Configure the workspace:
catkin config -DPYTHON_EXECUTABLE=/home/robot/anaconda3/envs/deeplearning/bin/python -DPYTHON_INCLUDE_DIR=/home/robot/anaconda3/envs/deeplearning/include/python3.6m -DPYTHON_LIBRARY=/home/robot/anaconda3/envs/deeplearning/lib/libpython3.6m.so. This example if for an environment named deeplearning which was created with Python 3.6
- Set install:
catkin config --install
- Create a src directory:
mkdir src && cd src
- Clone the vision_opencv repository:
git clone -b kinetic https://github.com/ros-perception/vision_opencv.git
- Build the project:
cd catkin_build_ws and catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF
- To make sure your system finds the library files run:
source install_isolated/setup.bash --extend
Update .bashrc file
To avoid having to constantly source the ROS workspace and load the library path, add the following lines to your ~/.bashrc file:
source ~/catkin_build_ws/install_isolated/setup.bash --extend and
Now you can import cv_bridge and import cv2 in Python 3 with ROS support.
To use this setup with PyCharm simply activate your conda environment within terminal. You may need to run
unset PYTHONPATH before opening PyCharm from this terminal.
2 thoughts on “Setting up ROS with Python 3 and OpenCV”
Hi! I found this post from a Google search, just for your information in 2021 installing ROS in conda should be much straightforward, see the following links:
View at Medium.com
ROS Noetic targets Python 3 unlike Kinetic which is built with Python 2.7. So the setup should me more straightforward for ROS Noetic.
LikeLiked by 1 person