0
$\begingroup$

Rosanswers logo

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?


Originally posted by Jägermeister on ROS Answers with karma: 81 on 2018-12-19

Post score: 0


Original comments

Comment by gvdhoorn on 2018-12-19:\

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.

Comment by gvdhoorn on 2018-12-19:
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.

Comment by Jägermeister on 2018-12-20:
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.

Comment by Jägermeister on 2018-12-20:
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.

Comment by gvdhoorn on 2018-12-20:
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 ..

Comment by gvdhoorn on 2018-12-20:
.. 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 ..

Comment by gvdhoorn on 2018-12-20:
.. 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.

Comment by gvdhoorn on 2018-12-20:\

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"?

Comment by Jägermeister on 2018-12-20:
That is only possible if you compile it from source as far as I know. If you have already installed ROS via sudo-apt, then you can't switch, or? Also, what if some nodes in the project require Python 2? Then what happens? We need a solution that can switch between these two, hence the idea here.

Comment by Jägermeister on 2018-12-20:
Not sure what you meant by email effect. Not gonna get any personal with you, I suggest you do the same with me.

Comment by gvdhoorn on 2018-12-20:\

That is only possible if you compile it from source as far as I know. If you have already installed ROS via sudo-apt, then you can't switch, or?

yes. But building from source is not as involved as it may seem. Basic steps are about 5 and then you're running make.

Comment by Jägermeister on 2018-12-20:
The problem is not only the process of compling it, rather the hassle that comes after, since you need to file patches for some issues. Who knows what will work and what won't, this kind of a risk I am not willing to take, as I don't want to spend time patching this and that.

Comment by Jägermeister on 2018-12-20:
I believe doing this via the way I think is the easiest. Requires no patch, no installation whatsoever, it's only a trick/hack that can save the situation. It works actually OK for the core Python 3 code but if you use packages (like tensorflow) then you face errors, which I try to resolve now.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Jägermeister with karma: 81 on 2018-12-20

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.