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

Altering PYTHONPATH in a ros node at runtime

asked 2018-12-19 09:01:28 -0500

Jägermeister gravatar image

updated 2018-12-19 09:02:11 -0500

Hi,

I'm running ROS kinetic and my code requires Python 3 to run. I've been told that ROS does not support Python 3 (which is terrible tbh) and I was looking for a way around, thus came up with the idea of changing the PYTHONPATH in the node, calling my file with using Python 2 subprocess.call() function, passing env with PYTHONPATH that points to Python 3. Theoretically, it should work. Needless to say, my program (the one that I want to invoke) works standalone.

So I sourced my setup.bash files to let ROS activate Python 2, which is OK. Node shall run in Python 2 anyway, only that it should call my file (written for Python 3) using such a mechanism:

# To find local version of the library
sys.path.append('path/to/local/lib')  

# run the code using a python 3 environment
python3 = '/usr/bin/python3'
execution_dir = '/path/to/my/code/dir'
command = [python3, execution_dir + "mycode.py", "-i " + "/tmp/image.png"]
subprocess.call(command)#, env={'PYTHONPATH': '/usr/lib/python3/dist-packages'})

However, this does not work. First, adding env causes the invoked program to crash with a Tensorflow error, which I don't usually get. This is probably because the PYTHONPATH is not properly set. (Any idea where exactly should it be?)

Even if I don't use env in the command, it does not work, this time it says it cannot find the local library, which is weird because I do add it (see the first line in the code).

Is there anyone who used a ROS node here with such a paradigm? How do you get something coded in Python3 running in ROS kinetic? Is there any workaround you could suggest?

edit retag flag offensive close merge delete

Comments

I've been told that ROS does not support Python 3 (which is terrible tbh)

did the person/persons that told you this also explain the situation? Did they also point you to the ongoing discussions, the open PRs and the proposed solutions / approaches?

It's easy to criticise, but switching is not.

gvdhoorn gravatar image gvdhoorn  ( 2018-12-19 14:33:55 -0500 )edit

Additionally: people using Gentoo, Arch and several other Linux distributions have been using Python 3 with ROS for quite some time now with quite some success.

I would perhaps suggest to first try doing that (ie: using Python 3), instead of tricking your PYTHONPATH with Python 2.

gvdhoorn gravatar image gvdhoorn  ( 2018-12-19 14:34:56 -0500 )edit

Regarding your first comment, in case you haven't noticed I am trying to come up with a solution here, instead of getting offended by users's comments about ROS, you can contribute to the thread. It's easy to write a comment, implementing a solution is not.

Jägermeister gravatar image Jägermeister  ( 2018-12-20 02:36:12 -0500 )edit

Regarding the second comment, you really shouldn't expect tens of people to switch their OS in a project just to get Python 3 work in ROS. Nevertheless, it was a better comment than the first, so you're progressing. Thanks.

Jägermeister gravatar image Jägermeister  ( 2018-12-20 02:37:12 -0500 )edit

I'm not suggesting you to switch OS. My comment was to let you know that there are communities (almost) of users that successfully have been able to use current ROS releases with Python 3. On Ubuntu this is not a common thing, but the OS I mentioned have to, as they don't support Python 2 ..

gvdhoorn gravatar image gvdhoorn  ( 2018-12-20 02:39:35 -0500 )edit

.. any longer, or it's severely deprecated on those platforms. So many pkgs have already been made Python 3 compatible, as necessitated on those OS. The suggestion was to at least try using ROS Kinetic with Python 3.

If you understood it as me saying that you should "just" change OS, then ..

gvdhoorn gravatar image gvdhoorn  ( 2018-12-20 02:40:30 -0500 )edit

.. that was not my intention.

re: getting offended: that was also not the intention, so please keep the "email effect" in mind.

I'm the first to agree that the current situation is undesirable and complicated, but unless someone steps up and does something, it's not going to change much soon.

gvdhoorn gravatar image gvdhoorn  ( 2018-12-20 02:44:21 -0500 )edit

Nevertheless, it was a better comment than the first, so you're progressing. Thanks.

Even with the email effect factored in, may I suggest you refrain from these types of "jests"?

gvdhoorn gravatar image gvdhoorn  ( 2018-12-20 02:49:36 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2018-12-20 04:40:50 -0500

Jägermeister gravatar image

I got it working at the end. The problem was the PYTHONPATH directory.

So, instead of using

/usr/lib/python3/dist-packages

I should have used:

/usr/local/lib/python3.6/dist-packages

This way, indeed, you can invoke a Python 3 program using Python 2.7 of ROS.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-12-19 09:01:28 -0500

Seen: 378 times

Last updated: Dec 20 '18