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

Run rospy inside Jython

asked 2021-06-28 14:44:33 -0600

Michdo93 gravatar image

Hi,

I installed Jython like this:

sudo java -jar jython-installer-2.7.0.jar
set this path --> /usr/local/lib/jython2.7.0/
sudo ln -s /usr/local/lib/jython2.7.0/bin/jython /usr/local/bin/jython
$ which jython
/usr/local/bin/jython
$ jython --version
Jython 2.7.0

I am using Python 2.7 and Ubuntu 18.04. I have installed ROS Kinetic Kame. But this does not matter. I could also have Melodic Morenia installed the problem is that I can not import Python Packages to Jython:

jython
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[OpenJDK 64-Bit Server VM (Azul Systems, Inc.)] on java1.8.0_252
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print('\n'.join(sys.path))

/usr/local/lib/jython2.7.0/Lib
__classpath__
__pyclasspath__/
/usr/local/lib/jython2.7.0/Lib/site-packages
>>> sys.path.append('/usr/lib/python2.7/dist-packages')
>>> print('\n'.join(sys.path))

/usr/local/lib/jython2.7.0/Lib
__classpath__
__pyclasspath__/
/usr/local/lib/jython2.7.0/Lib/site-packages
/usr/lib/python2.7/dist-packages
>>> sys.path.append('/usr/local/lib/python2.7/dist-packages')
>>> print('\n'.join(sys.path))

/usr/local/lib/jython2.7.0/Lib
__classpath__
__pyclasspath__/
/usr/local/lib/jython2.7.0/Lib/site-packages
/usr/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages
>>> import rospy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named rospy
>>>

A completely another approach was jython -m pip install rospy. But pip does not really work. Sadly.

The problem with pip is that it does not work in Jython:

jython -m pip install pip --upgrade

Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement pip in ./site-packages
Downloading/unpacking pip
Cleaning up...
No distributions at all found for pip in ./site-packages
Storing debug log for failure in /home/<user>/.pip/pip.log

If I look inside the log file I receive:

pip run on Mon Jun 28 20:32:41 2021
Getting page https://pypi.python.org/simple/pip/
Could not fetch URL https://pypi.python.org/simple/pip/: 403 Client Error: [[[!!! BREAKING CHANGE !!!]]] Support for clients that do not support Server Name Indication is temporarily disabled and will be permanently deprecated soon. See https://status.python.org/incidents/hzmjhqsdjqgb and https://github.com/pypa/pypi-support/issues/978 [[[!!! END BREAKING CHANGE !!!]]]
Will skip URL https://pypi.python.org/simple/pip/ when looking for download links for pip in ./site-packages
Getting page https://pypi.python.org/simple/
Could not fetch URL https://pypi.python.org/simple/: 403 Client Error: [[[!!! BREAKING CHANGE !!!]]] Support for clients that do not support Server Name Indication is temporarily disabled and will be permanently deprecated soon. See https://status.python.org/incidents/hzmjhqsdjqgb and https://github.com/pypa/pypi-support/issues/978 [[[!!! END BREAKING CHANGE !!!]]]
Will skip URL https://pypi.python.org/simple/ when looking for download links ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-06-28 15:36:24 -0600

gvdhoorn gravatar image

updated 2021-06-29 02:26:07 -0600

>>> print('\n'.join(sys.path))

/usr/local/lib/jython2.7.0/Lib
__classpath__
__pyclasspath__/
/usr/local/lib/jython2.7.0/Lib/site-packages
/usr/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages
>>> import rospy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named rospy

Seeing as ROS is installed in /opt/ros/$ROSDISTRO, with the Python packages all in /opt/ros/$ROSDISTRO/lib/python2.7/dist-packages (at least for ROS versions which use Python 2), it seems without that path on the sys.path it would be expected Jython can't import rospy.


Edit:

I'm not sure what you're getting at. Should I add to Jython /opt/ros/$ROSDISTRO?

No. If you had not built ROS from source, you would have had to add /opt/ros/$ROSDISTRO/lib/python2.7/dist-packages, as that's where all the Python packages live.

By the way I build it from source because Ubuntu 18.04 does not support ROS Kinetic so isntead of /opt/ros/$ROSDISTRO I should have to use ~/ros_catkin_ws.

This would be the right solution. But which is the correct one I have to use?

>>> sys.path.append('/home/openhabvm/ros_catkin_ws/build_isolated')
>>> sys.path.append('/home/openhabvm/ros_catkin_ws/devel_isolated')
>>> sys.path.append('/home/openhabvm/ros_catkin_ws/install_isolated')
>>> import rospy
>>>

Build, Devel or Install?

If you have an install space, you must use it. If you don't have an install space (so only a devel), then use the devel space.

In your case that's install_isolated, as you probably used catkin_make_isolated to build your workspace.

I am amazed. It didn't work if I left out individual things, and somehow I also have to consider an appropriate order. One also needs:

>>> sys.path.append('/home/openhabvm/ros_catkin_ws')
>>> sys.path.append('/home/openhabvm/ros_catkin_ws/src')

I would not expect this to be needed, as it isn't when using a regular Python interpreter. But perhaps Jython is different.

and:

>>> sys.path.append('/usr/local/lib/python2.7/dist-packages')

It could be this is needed if Jython is not using the default dist-packages (ie: the one managed by apt) and you've installed certain ROS Python dependencies manually in /usr/local.

The build-ROS-from-sources instructions do not do that though, so that would have been something you've done yourself.

Well it is not the whole ROS integration. I can not import std_msgs as example. I got this error:

>>> from std_msgs.msg import String
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name String

Of course, I also have a suspicion where this comes from. But I won't be the only one who first comes up with one solution and then looks for the next. It may be possible to link it later, should my project succeed.

As I don't know exactly what you've done, I can't comment on this. std_msgs is a pretty ... (more)

edit flag offensive delete link more

Comments

I'm not sure what you're getting at. Should I add to Jython /opt/ros/$ROSDISTRO? By the way I build it from source because Ubuntu 18.04 does not support ROS Kinetic so isntead of /opt/ros/$ROSDISTRO I should have to use ~/ros_catkin_ws.

This would be the right solution. But which is the correct one I have to use?

>>> sys.path.append('/home/openhabvm/ros_catkin_ws/build_isolated')
>>> sys.path.append('/home/openhabvm/ros_catkin_ws/devel_isolated')
>>> sys.path.append('/home/openhabvm/ros_catkin_ws/install_isolated')
>>> import rospy
>>>

Build, Devel or Install?

Michdo93 gravatar image Michdo93  ( 2021-06-28 16:39:26 -0600 )edit

I am amazed. It didn't work if I left out individual things, and somehow I also have to consider an appropriate order. One also needs:

>>> sys.path.append('/home/openhabvm/ros_catkin_ws')
>>> sys.path.append('/home/openhabvm/ros_catkin_ws/src')

and:

>>> sys.path.append('/usr/local/lib/python2.7/dist-packages')
Michdo93 gravatar image Michdo93  ( 2021-06-29 01:45:12 -0600 )edit

Well it is not the whole ROS integration. I can not import std_msgs as example. I got this error:

>>> from std_msgs.msg import String
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name String

Of course, I also have a suspicion where this comes from. But I won't be the only one who first comes up with one solution and then looks for the next. It may be possible to link it later, should my project succeed.

Michdo93 gravatar image Michdo93  ( 2021-06-29 02:00:08 -0600 )edit

I hope no one minds if I expand on this a bit. I could still use some help. Of course it is a different if I run jython or jython -Dpython.path=$PYTHONPATH.

echo $PYTHONPATH
/home/<user>/naoqi/pynaoqi-python2.7-2.5.7.1-linux64:/home/<user>/catkin_ws/devel/lib/python2.7/dist-packages:/home/<user>/ros_catkin_ws/install_isolated/lib/python2.7/dist-packages

$ROS_PACKAGE_PATH
/home/<user>/catkin_ws/src:/home/<user>/ros_catkin_ws/install_isolated/share

Another difference is if I use sys.path.insert or sys.path.append. With insert it seems to be possible to import modules and with append packages. I need to import ROS completely not only a part of it. Because I want to achieve to use ROS in Jython and this means it needs the same behaviour than in Python.

Michdo93 gravatar image Michdo93  ( 2021-06-29 04:15:51 -0600 )edit

Inside ~/ros_catkin_ws/install_isolated/include are header files for C++. In ~/ros_catkin_ws/devel_isolated we will find maybe the right modules but I do not know how to import it correctly.

ls ~/ros_catkin_ws/devel_isolated/std_msgs/lib/python2.7/dist-packages/std_msgs
__init__.py  __init__.pyc  msg

and

ls ~/ros_catkin_ws/devel_isolated/std_msgs/lib/python2.7/dist-packages/std_msgs/msg
_Bool.py             _Duration.pyc           _Header.py            _Int32.pyc                _MultiArrayLayout.py   _UInt32MultiArray.pyc
_Bool.pyc            _Empty.py               _Header.pyc           _Int64MultiArray.py       _MultiArrayLayout.pyc  _UInt32.py

...

What remains is that you would have to import all paths individually. So if not std_msgs maybe you have to import common_msgs etc. also with sys.path.append. Trying to achieve to use once the append function should lead to the fact that you only ever get from ... import, no matter which Module/Package

Michdo93 gravatar image Michdo93  ( 2021-06-29 04:26:29 -0600 )edit

I'm not sure whether this is Jython specific behaviour.

For a regular Python interpreter (ie: the one distributed via apt on Ubuntu 18.04), it would suffice to add the base dist-packages to the PYTHONPATH.

sys.path is a Python list. The append(..) and insert(..) methods basically modify that list. The former will add to the end of the list, while the latter allows to you insert new entries in arbitrary locations in that list.

I don't understand how that's related to adding packages or modules. It should not make any difference.

I'm also not sure what the relevance is of ROS_PACKAGE_PATH. It's not used by Python interpreters.

And again: if you have an install space, use that. It's not guaranteed that your devel space will be setup correctly in that case.

gvdhoorn gravatar image gvdhoorn  ( 2021-06-29 04:59:16 -0600 )edit

@gvdhoorn Thank you for your explanation and help. I think in principle I was on the wrong track anyway. When you import rospy, it wants to work with python either way. So you would actually have to develop some kind of rosjy to solve that. I can't just run that under jython instead of python, I guess. I'm at least right about the imported directory.

python
Python 2.7.17 (default, Feb 27 2021, 15:10:58)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more informati                                                                                                on.
>>> import rospy
>>> import imp
>>> imp.find_module('rospy')
(None, '/home/<user>/ros_catkin_ws/install_isolated/lib/python2.                                                                                                7/dist-packages/rospy', ('', '', 5))
Michdo93 gravatar image Michdo93  ( 2021-06-29 05:36:09 -0600 )edit

I try to explain what I mean:

jython talker.py
Traceback (most recent call last):
  File "talker.py", line 39, in <module>
    import rospy
ImportError: No module named rospy

Here I can not find the rospy. Of course if I use python instead of jython it works. So adding the $PYTHONPATH would help a little bit:

jython -Dpython.path=$PYTHONPATH talker.py
Traceback (most recent call last):
  File "talker.py", line 39, in <module>
    import rospy
  File "/home/openhabvm/ros_catkin_ws/install_isolated/lib/python2.7/dist-packages/rospy/__init__.py", line 47, in <module>
    from std_msgs.msg import Header
  File "/home/openhabvm/ros_catkin_ws/install_isolated/lib/python2.7/dist-packages/std_msgs/msg/__init__.py", line 1, in <module>
    from ._Bool import *
  File "/home/openhabvm/ros_catkin_ws/install_isolated/lib/python2.7/dist-packages/std_msgs/msg/_Bool.py", line 6, in <module>
...

Now it is possible ti import rospy.

Michdo93 gravatar image Michdo93  ( 2021-06-29 05:39:26 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2021-06-28 14:44:33 -0600

Seen: 213 times

Last updated: Jun 29 '21