I tried running the following command using subprocess.Popen():
ros_version_retrieve = subprocess.Popen(["rosversion", "-d"], shell=False, stdout=subprocess.PIPE)
ros_version_retrieve.wait()
rosversion is part of ROS and allows retrieving either the version of a given package by calling rosversion <package> or the version of the version of the installed ROS by calling rosversion -d. In the first case I will get a string like 1.12.3 and in the second I get kinetic (since that is the release I am using at the moment).
I launch Eclipse from the terminal so that PATH and a couple of other variables are also available in the IDE:
PYTHONPATHis /usr/local/lib/python2.7/dist-packages:/home/user/catkin_ws/devel/lib/python2.7/dist-packages:/opt/ros/kinetic/lib/python2.7/dist-packages:/usr/local/lib/python3.5/dist-packages/:/opt/OpenCV/python/3.4:/opt/OpenCV/python/2.7:/opt/ros/indigo/lib/python2.7/dist-packagesPATHis /opt/Qts/5.9/bin:/opt/QtCreator-custom/bin:/usr/local/bin:/opt/MATLAB/R2012a/bin/:/home/user/bin:/home/user/catkin_ws/scripts:/opt/ros/kinetic/bin:/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/Qts/5.9/bin:/snap/bin:/usr/lib/jvm/java-9-oracle/bin:/usr/lib/jvm/java-9-oracle/db/bin
I know it's quite untidy (I've just learned about virtualenv) but it has worked so far without any issues.
When I run the first that same line of code from inside PyDev (Eclipse Neon) I get
Fatal Python error: Py_Initialize: Unable to get the locale encoding
File "/usr/lib/python2.7/encodings/init.py", line 123 raise CodecRegistryError,\^ SyntaxError: invalid syntax
I also tried calling rosversion with a specific package namely
subprocess.Popen(["rosversion", "roscpp"], shell=False, stdout=subprocess.PIPE)
but I got the same error. Both command calls work without a single issue in my terminal and also when I call my script outside of PyDev.
I have followed this question looking for answers. Unsetting my PYTHONPATH and then manually setting it inside my code is not an option. I am also pretty sure that the command is found since I have multiple other locations in my code where I call rospack with a combination of basically every argument it offers and it works just fine. From the error and also the settings of my PyDev project I'm also sure that the correct interpreter is called namely the one for Python 2.7 and not for 3.4 which I also have on my system.
Since this seems (at least in my case) to be a PyDev problem rather than a system-wide problem I'm looking for a solution where I don't have to change my script just to be able to run it inside PyDev. If there is a project setting I need to set, do tell.
UPDATE:
Thanks for the answer below. However popping PYTHONPATH leads to rosversion failing but this time due to the inability to import rospkg (which is part of the ROS environment that is loaded through the PYTHONPATH) internally. I don't have import rospkg anywhere in my code since that defeats the purpose of what I'm doing - an external configuration tool for ROS that can even set it up, which would be impossible if I import and use modules that are part of it.
My PYTHONHOME is actually empty. If I add only the encoding part that was suggested the same error occurs as before - PyInitialize, CodecRegistryError etc. etc.
Again - the script works without any issues if I start it from bash. It fails ONLY when I run it from PyDev.