ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How do you use Python3 and its libraries with ROS Melodic (researched)?

asked 2019-05-21 09:13:45 -0600

SnowyGrizzly gravatar image

updated 2022-01-22 16:09:57 -0600

Evgeny gravatar image

Hi, I'm a total ROS newbie and I'm looking for help with integrating SPADE multi-agent Python library (3.6 and up) with ROS Melodic (Ubuntu 18.04). I'm really desperate right now because I was looking for many different anwsers how to do such a thing. This is a precise description of what I did and with many many different attempts before:

Creating the main package:

  1. I create folders catkin_ws/src and run the command catkin_make in catkin_ws to create a workspace.
  2. I create the first package named spade_test in which the main .py files will be located, I use catkin_create_pkg spade_test std_msgs rospy in the src folder.
  3. Every time I change something significant, I run catkin_make and . ~/catkin_ws/devel/setup.bash commands.
  4. I add <build_depend>message_generation</build_depend> and <exec_depend>message_runtime</exec_depend> to package.xml file and message_generation in find_package section and CATKIN_DEPENDS message_runtime in catkin_package section of CMakeLists.txt file. I also uncomment the generate_messages section (don't think anything in this step is essential).
  5. I add 2 files talker.py and listener.py to the spade_test folder, the same code as in this tutorial.
  6. I'm using the chmod + x filename.py command to make these work as executable files.

Creating package with a library inside:

  1. I create the spade package by using the catkin_create_pkg spade std_msgs rospy command in the src folder.

  2. In the package.xml file in the <version> tag, I change it to 3.1.2, while in the CMakeLists.txt file I uncomment the catkin_python_setup() line.

  3. I git clone everything from the SPADE repository to the spade folder (setup.py file included).

  4. In spade_test package's package.xml file I add 3 lines: <build_depend>spade</build_depend>, <build_export_depend>spade</build_export_depend> and <exec_depend>spade</exec_depend>. In its CMakeLists.txt I add spade to find_package section.

  5. After using catkin_make and . ~/catkin_ws/devel/setup.bash commands, the talker.py and listener.py files work fine after using rosrun spade_test talker.py etc.

  6. I added from spade.agent import Agent to test it if the library works properly and sadly it doesn't, the error occured:

Error

Traceback (most recent call last):
  File "/home/my_name/catkin_ws/src/spade_test/talker.py", line 6, in <module>
    from spade.agent import Agent
  File "/home/my_name/catkin_ws/devel/lib/python2.7/dist-packages/spade/__init__.py", line 35, in <module>
    exec(__fh.read())
  File "<string>", line 3, in <module>
  File "/home/my_name/catkin_ws/src/spade/spade/agent.py", line 86
    async def _async_start(self, auto_register=True):
            ^
SyntaxError: invalid syntax

It's definitely related to the wrong version of Python my ROS is using (2.7) and I was looking everywhere to change it (even some answers from this place) but sadly nothing has helped. I also created another catkin workspace with catkin build --cmake-args -DPYTHON_VERSION=3.6 command instead of catkin_make with the exact same result (except with /home/my_name/catkin_ws/devel/lib/python3/dist-packages/spade/__init__.py line in the error instead).

I installed my ROS Melodic with this tutorial ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
-1

answered 2019-06-06 03:39:48 -0600

twdragon gravatar image

updated 2019-06-06 03:40:19 -0600

In our projects we found a workaround for this problem. It is pretty easy to retain the Python interpreter which the ROS installation uses. There is ROS_PYTHON_VERSION environmental variable defined in 1.ros_python_version.sh file located inside your ROS installation. To change the version it is enough to run something like the following (in Ubuntu or inherited distributions):

sudo ROS_INSTALL_DIR=/opt/ros/melodic sed -i -e 's/ROS_PYTHON_VERSION=2/ROS_PYTHON_VERSION=3/' ${ROS_INSTALL_DIR}/etc/catkin/profile.d/1.ros_python_version.sh

If you installed ROS from sources it is highly recommended to recompile your Python/Cython dependent packages after retaining of used Python version.

edit flag offensive delete link more

Comments

Rewriting the Python version of an already built and installed workspace sounds like asking for trouble. I would recommend t not do this. Instead you might want to do a from source build while selecting a Python 3 interpreter.

Dirk Thomas gravatar image Dirk Thomas  ( 2019-06-06 10:52:34 -0600 )edit

Surely, it should not be recommended for installed workspace. In trouble with prebuilt ROS packages it could be useful to try to fix version incompatibility

twdragon gravatar image twdragon  ( 2019-06-06 11:24:58 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2019-05-21 09:13:45 -0600

Seen: 3,878 times

Last updated: Jun 06 '19