Run rospy inside Jython
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 for pip in ./site-packages
Cannot fetch index base URL https://pypi.python.org/simple/
URLs to search for versions for pip in ./site-packages:
* https://pypi.python.org/simple/pip/
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
Could not find any downloads that satisfy the requirement pip in ./site-packages
Downloading/unpacking pip
Cleaning up...
Removing temporary dir /tmp/pip_build_<user>...
No distributions at all found for pip in ./site-packages
Exception information:
Traceback (most recent call last):
File "/usr/local/lib/jython2.7.0/Lib/site-packages/pip/basecommand.py", line 133, in main
status = self.run(options, args)
File "/usr/local/lib/jython2.7.0/Lib/site-packages/pip/commands/install.py", line 316, in run
requirement_set.prepare_files(
File "/usr/local/lib/jython2.7.0/Lib/site-packages/pip/commands/install.py", line 316, in run
requirement_set.prepare_files(
File "/usr/local/lib/jython2.7.0/Lib/site-packages/pip/req/req_set.py", line 327, in prepare_files
raise not_found
DistributionNotFound: No distributions at all found for pip in ./site-packages
Maybe you have experience in working with Jython and ROS. I know this is not a ROS problem. I will include ROS inside a Java application with Jython. Jython is working inside my Java application. And of course outside of the catkin workspace Python is working. So my thought was to combine it with Jython. Maybe I should give rosjava a try because it is ROS Kinetic.
Asked by Michdo93 on 2021-06-28 14:44:33 UTC
Answers
>>> 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 low-level dependency though, so if you can import rospy
, I would expect std_msgs
to also be present.
Edit 2: I'm wondering whether it wouldn't have been simpler to use rospypi/simple to setup a self-contained, minimal virtualenv
with just rospy
and some extra packages, instead of building ROS Kinetic from source on Ubuntu Bionic.
Asked by gvdhoorn on 2021-06-28 15:36:24 UTC
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?
Asked by Michdo93 on 2021-06-28 16:39:26 UTC
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')
Asked by Michdo93 on 2021-06-29 01:45:12 UTC
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.
Asked by Michdo93 on 2021-06-29 02:00:08 UTC
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.
Asked by Michdo93 on 2021-06-29 04:15:51 UTC
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
Asked by Michdo93 on 2021-06-29 04:26:29 UTC
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.
Asked by gvdhoorn on 2021-06-29 04:59:16 UTC
@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))
Asked by Michdo93 on 2021-06-29 05:36:09 UTC
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.
Asked by Michdo93 on 2021-06-29 05:39:26 UTC
*to
But what is not possible is that rospy
as example loads other imports
. So I can not run genpy
which is needed and so one. This does lead me to believe that I can't run ROS with Jython without writing my own packages, which are then actually almost the same as the Python code. Or am I missing something?
Asked by Michdo93 on 2021-06-29 05:41:40 UTC
I've just tried this myself (ie: download Jython, run it in a Docker container with ROS Melodic), and what seems to be happening is that Jython's default sys.path
just doesn't include the system's Python packages (ie: dist-packages
, etc). That's why you need to add all sorts of folders there manually.
In addition: I just noticed that Jython's most recent version is 2.7.2
. I don't know whether Jython implements some sort of forward compatibility, but the Python versions of both ROS Melodic and ROS Kinetic are much more recent than that (for Melodic it's 2.7.17
fi). I would not be surprised if some of the problems you're running into are caused by that difference.
When setting JYTHONPATH=$PYTHONPATH
(and adding some standard system locations) I don't get import errors any more, but PyYaml
doesn't want to load, as it seems there is something not working in the old 2.7.2
version of Python which Jython supports: assert a.itemsize == _sre.CODESIZE
.
Asked by gvdhoorn on 2021-06-29 06:06:46 UTC
It's probably not necessarily a good idea to add the regular Python packages to Jython's sys.path
. The binary compatibility alone may make that not work.
When you import rospy, it wants to work with python either way.
I don't know what you mean by this.
None of the rospy
modules will force you to use python
. Only scripts will refer to /usr/bin/env python
.
I don't know whether this is Jython specific or not. In any case, it doesn't seem like there's anything wrong with the ROS Python packages.
As I don't use any of this, I'm afraid I can't help you any further.
Asked by gvdhoorn on 2021-06-29 06:09:15 UTC
I tested all standalone Jython jar files. In none of this it works. Sadly...
sudo java -jar jython-standalone-2.7-b2.jar -Dpython.path=$PYTHONPATH
Jython 2.7b2 (default:a5bc0032cf79+, Apr 22 2014, 21:20:17)
[OpenJDK 64-Bit Server VM (Azul Systems, Inc.)] on java1.8.0_252
Type "help", "copyright", "credits" or "license" for more information.
>>> import rospy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/<user>/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/<user>/ros_catkin_ws/install_isolated/lib/python2.7/dist-packages/std_msgs/msg/__init__.py", line 1, in <module>
from ._Bool import *
File "/home/<user>/ros_catkin_ws/install_isolated/lib/python2.7/dist-packages/std_msgs/msg/_Bool.py", line 6, in <module>
import genpy
File "/home/<user>/ros_catkin_ws/install_isolated/lib/pyth
Asked by Michdo93 on 2021-06-29 07:17:23 UTC
And in older versions:
sudo java -jar jython-standalone-2.7-b1.jar -Dpython.path=$PYTHONPATH
Jython 2.7b1 (default:ac42d59644e9, Feb 9 2013, 15:24:52)
[OpenJDK 64-Bit Server VM (Azul Systems, Inc.)] on java1.8.0_252
Type "help", "copyright", "credits" or "license" for more information.
>>> import rospy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/<user>/ros_catkin_ws/install_isolated/lib/python2.7/dist-packages/rospy/__init__.py", line 62
SyntaxError: 'import *' not allowed with 'from .'
Asked by Michdo93 on 2021-06-29 07:17:57 UTC
Comments