PyDev messing encoding settings when running Python script from inside Eclipse

余生长醉 提交于 2019-12-12 05:49:02

问题


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:

  • PYTHONPATH is /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-packages
  • PATH is /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.


回答1:


I think that the problem is that you're mixing Python 2 and Python 3 paths in the configuration.

i.e.: From your PYTHONPATH you have:

/usr/local/lib/python2.7/dist-packages 
/home/user/catkin_ws/devel/lib/python2.7/dist-packages <-- py2
/opt/ros/kinetic/lib/python2.7/dist-packages
/usr/local/lib/python3.5/dist-packages/  <- py3
/opt/OpenCV/python/3.4:/opt/OpenCV/python/2.7 
/opt/ros/indigo/lib/python2.7/dist-packages <-- py2

I think that to be safe you should create the environment you want and pass it to the subprocess call, properly filling the keys:

  • PYTHONPATH
  • PYTHONHOME
  • PYTHONIOENCODING
  • PYTHONUNBUFFERED

Something as:

env = os.environ.copy()
env.pop('PYTHONPATH', None)
env.pop('PYTHONHOME', None)
env['PYTHONIOENCODING'] = 'ascii'
env['PYTHONUNBUFFERED'] = '1'
subprocess.Popen(["rosversion", "-d"], env=env)

Maybe PATH and LANG could also be added to that list (not sure, it depends a lot on what you're launching and which variables the program you use will read).




回答2:


I just tried something out and it worked - the command list for my Popen() looks like this: ["python2.7", "/usr/local/bin/rosversion", "-d"] and it works!

Because of problems accessing GitHub I was actually unable to access the source code of rosversion although I was blind enough not to come to the conclusion myself by looking at the error of the failed import for rospkg.

Unlike rospack rosversion is a Python script. While calling it inside a bash shell doesn't lead to a problem it seems that running it inside a subprocess requires for me to actually invoke the Python interpreter inside that subprocess and pass the absolute path (which I can get using which rosversion).



来源:https://stackoverflow.com/questions/45799999/pydev-messing-encoding-settings-when-running-python-script-from-inside-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!