Altering PYTHONPATH in a ros node at runtime
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?
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.
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.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.
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.
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 ..
.. 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 ..
.. 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.
Even with the email effect factored in, may I suggest you refrain from these types of "jests"?