rickstaa/ros-gazebo-gym-ws

ROS python 3 conflicts

Closed this issue · 0 comments

As python 2.7 has reached its EOL. I will develop mostly in python 3. Since ROS currently does not fully support python3 I ran into some python 2/3 compatibility issues when I was using ROS. These issues will be fixed when ROS Noetic is released in May. After Noetic is released, the repository can be ported over to use python3 (see this porting guide). At the moment I however still, however, have to use ROS Melodic which has not yet been fully ported to python3. Instead of compiling ROS melodic for python3, I can separate the RL training algorithm (python3) and the main ROS system (python 2). To do this I need to create a virtual environment for the training algorithm and a catkin_ws in which I compile required ROS packages for python3.

Create a virtual environment

To create a virtual environment use the following commands:

pip install virtualenv 
virtualenv ~/.catkin_ws_python3/openai_venv --python=python3 

This virtual environment can the be activated using the source ~/.catkin_ws_python3/openai_venv/bin/activate command. After the environment is activate you you have to install the following python packages in it:

pip install tensorflow-gpu
pip install gym
pip install pyyaml
pip install netifaces
pip install rospkg

Build the required ROS packages from source

In the panda training script I will need the following ROS (python3) packages:

These are compiled for python3 by using the rosinstall_generator tool togheter with the wstool. To recompile for python3 (melodic):

Install some prerequisites to use Python3 with ROS.

sudo apt update
sudo apt install python3-catkin-pkg-modules python3-rospkg-modules python3-empy

Prepare catkin workspace:

cd ~/.catkin_ws_python3
mkdir src
ROS_PYTHON_VERSION=3
wstool init src
rosinstall_generator ros_comm common_msgs  geometry2 --rosdistro melodic --deps | wstool merge -t src -
wstool update -t src -j8
rosdep install --from-paths src --ignore-src -y -r

Finally compile for Python 3:

catkin build --cmake-args \
            -DCMAKE_BUILD_TYPE=Release \
            -DPYTHON_EXECUTABLE=/usr/bin/python3 \
            -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m \
            -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so

While trying to build the packages you might get the following error:

File "/home/ricks/.catkin_ws_python3/src/orocos_kinematics_dynamics/python_orocos_kdl/cmake/FindSIP.py", line 8, in <module>
    import sipconfig
ModuleNotFoundError: No module named 'sipconfig'
CMake Error at cmake/FindSIP.cmake:63 (MESSAGE):
  Could not find SIP
Call Stack (most recent call first):
  CMakeLists.txt:21 (find_package)
-- Configuring incomplete, errors occurred!

This error is resolved by installing the python3-pyqt5 python3-sip python3-sip-dev python3-empy packages. Additionally, if you also want to use the ROS command line commands you need to install the following dependencies in your virtual environment:

sudo apt install python3-pycryptodome python3-gnupg

Resources