5. Building ROS Melodic with Python 3 Support - sundevilrobotics/urc-code GitHub Wiki

After a tremendous amount of testing and research, I finally found a way to compile a ROS Melodic catkin workspace with Python 3 support. Here are the steps (thanks to https://dhanoopbhaskar.com/blog/2020-05-07-working-with-python-3-in-ros-kinetic-or-melodic/ for help/inspiration!):

  1. If you run catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3 in ~/catkin_ws, you should get an error such as CMake Error from No module named 'catkin_pkg' or something similar. If it compiles successfully, congratulations, you already have your catkin workspace working with Python 3.
  2. Run sudo apt install python3-pip python3-all-dev python3-rospkg. This will uninstall ROS (since it only has support for Python 2), so we will need to reinstall ROS and all of our ROS package dependencies in the following steps.
  3. Run sudo apt install ros-melodic-desktop-full --fix-missing with the ROS install type (desktop-full, desktop, or bare-bones) of your choosing. This will install ROS again with the correct Python 3 (and Python 2) support.
  4. cd ~/catkin_ws/src/urc-code and run chmod +x install.sh and then ./install.sh again to reinstall our urc-code dependencies.
  5. cd ~/catkin_ws/ and run catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3 again. You should see the line -- Found PythonInterp: /usr/bin/Python3 (found version "3.X.X") near the top of the catkin_make output. If the catkin_make completes without errors, you have successfully compiled your catkin workspace with Python 3 support!

Additionally, I also needed to:

  1. Run sudo apt install python3-empy to fix an compilation error (ImportError: No module named 'em')

If you ever need to compile with Python 2 in the future, you can switch back by running a catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python2.7 or maybe a catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python2 or even potentially a catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python.

Please continue to contribute to this page, updating it with additional dependencies needed like the python3-empy package installed in step 6.