How do you use Python3 and its libraries with ROS Melodic (researched)?
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:
- I create folders
catkin_ws/src
and run the commandcatkin_make
incatkin_ws
to create a workspace. - I create the first package named
spade_test
in which the main .py files will be located, I usecatkin_create_pkg spade_test std_msgs rospy
in thesrc
folder. - Every time I change something significant, I run
catkin_make
and. ~/catkin_ws/devel/setup.bash
commands. - I add
<build_depend>message_generation</build_depend>
and<exec_depend>message_runtime</exec_depend>
topackage.xml
file andmessage_generation
infind_package
section andCATKIN_DEPENDS message_runtime
incatkin_package
section ofCMakeLists.txt
file. I also uncomment thegenerate_messages
section (don't think anything in this step is essential). - I add 2 files
talker.py
andlistener.py
to thespade_test
folder, the same code as in this tutorial. - I'm using the
chmod + x filename.py
command to make these work as executable files.
Creating package with a library inside:
I create the
spade
package by using thecatkin_create_pkg spade std_msgs rospy
command in thesrc
folder.In the
package.xml
file in the<version>
tag, I change it to3.1.2
, while in theCMakeLists.txt
file I uncomment thecatkin_python_setup()
line.I git clone everything from the SPADE repository to the
spade
folder (setup.py
file included).In
spade_test
package'spackage.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 itsCMakeLists.txt
I addspade
tofind_package
section.After using
catkin_make
and. ~/catkin_ws/devel/setup.bash
commands, thetalker.py
andlistener.py
files work fine after usingrosrun spade_test talker.py
etc.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.
I would be really grateful if someone would help me with all of this as there might be some mistakes earlier which I'm not even aware of.
Asked by SnowyGrizzly on 2019-05-21 09:13:45 UTC
Answers
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.
Asked by twdragon on 2019-06-06 03:39:48 UTC
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.
Asked by Dirk Thomas on 2019-06-06 10:52:34 UTC
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
Asked by twdragon on 2019-06-06 11:24:58 UTC
Comments