0
$\begingroup$

Rosanswers logo

System information

  • OS: Ubuntu 18.04
  • ROS version: Melodic
  • Conda environment: python=3.7 (Needed for a python3 processing module)

Problem explanation

I try to create a message filter that subscribes to two sensor_msg topics after which a python3 module processes the Color and depth data in a callback function. To do this, I created the following script to achieve this:

#!/usr/bin/env python3

## Import standard library packages ##
import sys

## Import ROS python packages ##
import rospy
import sensor_msgs
from message_filters import ApproximateTimeSynchronizer, Subscriber

def gotimage(image, camerainfo):
    assert image.header.stamp == camerainfo.header.stamp
    print("got an Image and CameraInfo")

## Main script ##
# Only runs when this script is executed directly
if __name__ == "__main__":

    ## Initialize ros node ##
    rospy.init_node('grasp_planner', anonymous=True)
    image_sub = Subscriber("/kinect2/hd/image_color", sensor_msgs.msg.Image)
    camera_sub = Subscriber("/kinect2/hd/camera_info", sensor_msgs.msg.CameraInfo)

    ## Create message_filter ##
    ats = ApproximateTimeSynchronizer([image_sub, camera_sub], queue_size=5, slop=0.1)
    ats.registerCallback(gotimage)
    rospy.spin()

Unfortunately, after trying to run this script, I get the following error message:

/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/ricks/miniconda3/envs/panda_autograsp/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
[ERROR] [1566834367.342856]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7fb849e44f50>>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/opt/ros/melodic/lib/python2.7/dist-packages/message_filters/__init__.py", line 75, in callback
    self.signalMessage(msg)
  File "/opt/ros/melodic/lib/python2.7/dist-packages/message_filters/__init__.py", line 57, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/melodic/lib/python2.7/dist-packages/message_filters/__init__.py", line 282, in add
    for vv in itertools.product(*[zip(*s)[0] for s in stamps]):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/message_filters/__init__.py", line 282, in <listcomp>
    for vv in itertools.product(*[zip(*s)[0] for s in stamps]):
TypeError: 'zip' object is not subscriptable

Looks like the package released a pull request but it is not yet merged. In this, cause I'm unfortunately in a deadlock since my robot supports ROS up to version melodic and the image processing package is written in python3. Does anybody know what is the best way to solve my problem?

What I already tried

  • I tried setting the export ROS_PYTHON_VERSION=3 environmental variable.
  • Building the rospy from source.

Building rospy from source

  1. Clone the rospy package into your catkin_ws.
  2. Setting the export ROS_PYTHON_VERSION=3 environmental variable.
  3. Building the rospy package using the catkin_make -j4 -DCMAKE_BUILD_TYPE=Release -DFranka_DIR:PATH="$HOME/libfranka/build" -Dfreenect2_DIR="$HOME/freenect2/lib/cmake/freenect2" command.
  4. Source the devel/setup.bash file.

After these steps my python path becomes PYTHONPATH=/home/ricks/panda_autograsp_ws/devel/lib/python2.7/dist-packages:/opt/ros/melodic/lib/python2.7/dist-packages.

When I try to run the python3 node I now get the following error:

Traceback (most recent call last):
  File "/home/ricks/panda_autograsp_ws/src/rospy/src/rospy/client.py", line 100, in load_command_line_node_params
    if rosgraph.names.is_legal_remap(arg):
AttributeError: module 'rosgraph.names' has no attribute 'is_legal_remap'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ricks/panda_autograsp_ws/src/panda_autograsp/nodes/grasp_planner_node.py", line 39, in <module>
    rospy.init_node('grasp_planner', anonymous=True)
  File "/home/ricks/panda_autograsp_ws/src/rospy/src/rospy/client.py", line 326, in init_node
    _init_node_params(argv, name)
  File "/home/ricks/panda_autograsp_ws/src/rospy/src/rospy/client.py", line 183, in _init_node_params
    params = load_command_line_node_params(argv)
  File "/home/ricks/panda_autograsp_ws/src/rospy/src/rospy/client.py", line 107, in load_command_line_node_params
    raise rospy.exceptions.ROSInitException("invalid command-line parameters: %s"%(str(e)))
rospy.exceptions.ROSInitException: invalid command-line parameters: module 'rosgraph.names' has no attribute 'is_legal_remap'

Originally posted by rickstaa on ROS Answers with karma: 111 on 2019-08-26

Post score: 0


Original comments

Comment by gvdhoorn on 2019-08-26:\

Does anybody know what is the best way to solve my problem?

build rospy with the patch in your Catkin workspace (it wouldn't really be building, as rospy is all Python, but you'll need to do it anyway for Catkin to properly overlay the apt version with the version in your workspace)?

Btw: you appear to be mixing Python 2.x and Python 3 PYTHONPATHs. That is probably not a good idea.

Comment by rickstaa on 2019-08-26:
@gvdhoorn Thanks a lot for your answer. You mean that I have to build the ros_comm package (which includes the rospy module) from the source from within my catkin workspace as you explained in #252478 and #295012?

Btw: you appear to be mixing Python 2.x and Python 3 PYTHONPATHs. That is probably not a good idea.

My PYTHONPATH currently is /opt/ros/melodic/lib/python2.7/dist-packages since I sourced the devel/setup.bash of my workspace before running the python node. Do I need to remove this line when using a python3 ROS package?

Comment by rickstaa on 2019-08-26:\

Btw: you appear to be mixing Python 2.x and Python 3 PYTHONPATHs. That is probably not a good idea.

Ah, I now see what goes wrong. When I try to run the node in the python 3 virtual environment it gives me the No module named 'rospy' not found error.

Comment by gvdhoorn on 2019-08-26:
You don't need to build all of ros_comm most likely. Copying out just the rospy client library package should be sufficient (not very maintainable, but it should work).

Comment by rickstaa on 2019-08-26:
@gvdhoorn Thanks for your explanation. I tried building the rospy module from source but I now get a new attribute error. The steps I performed while doing this can be found appended to the question above.

The other strange thing is that the python3 package still ends up in the /home/ricks/panda_autograsp_ws/devel/lib/python2.7/dist-packages folder instead of the /home/ricks/panda_autograsp_ws/devel/lib/python3.7/dist-packages.

Comment by gvdhoorn on 2019-08-27:
I don't believe that is strange: from what you show here it would appear you're still mixing Python 2 and Python 3 (your Conda environment). Without special care, Catkin will use Python 2. sourceing a workspace from inside your Conda environment will not change that.

Note that the patch only makes that particular part of rospy language-compatible with Python 3, if you're still using Python 2, Python 2 will still be used.

You may try the procedure outlined here, but from within your Conda environment instead of a plain venv.

No guarantees, but it could work.

Comment by rickstaa on 2019-08-29:
@gvdhoorn thanks a lot for the explanation. I will do some testing and report my solution back as an answer.

Comment by rickstaa on 2019-08-31:
@gvdhoorn As I managed to use the package with python 2.7, after all, I didn't test the procedure you outlined in this topic. I, however, added an answer based on my current findings below so I can close this question. Although I know I saw a python 3 transitioning status REP for the ROS main packages (see the tag) somewhere I was unable to find it again. Do you maybe know where I can find such an overview? Feel free to correct me if I misunderstood something in my answer below.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

As a package I was using recommended python3 I tried to get ROS to work with Python3. Below I summarised my findings while doing this.

Overal ROS python 3 support status

A list of ROS distributions and their python-support status can be found here. A guide on how to use python3 with rose can be found in the UsingPython3 page of the ROS documentation.

ROS kinetic with python 3

Since at the time I started building my package the franka_ros wasn't released for melodic I had to use kinetic. However, since ROS kinetic does not fully support python3 getting the GQCNN to work under ubuntu 16.04 ROS Kinetic is not easy. I kept running into problem after problem. In the end, I decided to contribute python 2/python 3 compatibility fixed to the python 3 repositories and use python 2.7 instead. If other people need to work with python 3 and ROS kinetic, please refer to this ROS issue.

ROS melodic with python 3

As stated in the distribution list ROS, ROS Python packages starting with Melodic are highly encouraged to support both Python 2.7 and Python 3.5 or later. As a result, better ROS Melodic python 3 support is on its way. The core packages in ROS melodic are currently being ported from python 2 to python 3. If you experience a python 2/3 incompatibility issue, you can check the corresponding package repository at (https://github.com/ros)[https://github.com/ros] and file an issue. If you, have to use a package that is not yet ported to python 3 you have to build this package from source. As explained by @gvdhoorn, the procedure of doing this can be found here.

Additional Resources


Originally posted by rickstaa with karma: 111 on 2019-08-31

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.