从Eclipse内部运行Python脚本时的PyDev混乱编码设置 [英] PyDev messing encoding settings when running Python script from inside Eclipse

查看:87
本文介绍了从Eclipse内部运行Python脚本时的PyDev混乱编码设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 subprocess.Popen()运行以下命令:

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 是ROS的一部分,可以通过调用<$来检索给定软件包的版本c $ c> rosversion< package> 或调用 rosversion -d 来安装已安装的ROS版本。在第一种情况下,我将得到像 1.12.3 这样的字符串,在第二种情况下,我将得到 kinetic (因为这是

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).

我从终端启动Eclipse,以便 PATH 和几个IDE中还可以使用其他变量:

I launch Eclipse from the terminal so that PATH and a couple of other variables are also available in the IDE:


  • PYTHONPATH /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-软件包

  • PATH /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:/快照/ bin:/ usr / lib / jvm / java-9-oracle / bin:/ u sr / lib / jvm / java-9-oracle / db / bin

  • 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

我知道这很不整洁(我ve刚刚了解了 virtualenv ),但是到目前为止它一直没有任何问题。

I know it's quite untidy (I've just learned about virtualenv) but it has worked so far without any issues.

当我运行第一个从PyDev(Eclipse Neon)内部获得同一行代码

When I run the first that same line of code from inside PyDev (Eclipse Neon) I get


致命的Python错误:Py_Initialize:无法获取区域设置编码

文件 /usr/lib/python2.7/encodings/init.py,第123行
引发CodecRegistryError,\

Fatal Python error: Py_Initialize: Unable to get the locale encoding
File "/usr/lib/python2.7/encodings/init.py", line 123 raise CodecRegistryError,\

^ SyntaxError:语法无效

^ SyntaxError: invalid syntax

我也尝试调用 rosversion 具有特定包

I also tried calling rosversion with a specific package namely

subprocess.Popen(["rosversion", "roscpp"], shell=False, stdout=subprocess.PIPE)

但我遇到了同样的错误。在我的终端中以及在PyDev之外调用脚本时,这两个命令调用都可以正常工作。

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.

我遵循了问题以寻找答案。取消设置我的 PYTHONPATH ,然后在我的代码中手动设置它不是一种选择。我也很确定找到了该命令,因为我的代码中还有多个其他位置,我在其中调用 rospack 并结合了它提供的每个基本参数,并且效果很好。从错误以及我的PyDev项目的设置中,我还可以确保调用了正确的解释器,即用于Python 2.7的解释器,而不是用于系统上的3.4的解释器。

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.

因为这似乎(至少在我看来)是PyDev问题,而不是系统范围的问题,所以我正在寻找一种解决方案,无需为了运行而不必更改脚本它在PyDev中。

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.

更新:

感谢您的回答。但是,弹出 PYTHONPATH 会导致 rosversion 失败,但这一次是由于无法导入 rospkg (是通过 PYTHONPATH 加载的ROS环境的一部分)。我在代码中的任何地方都没有 import rospkg ,因为这违反了我正在做的目的-ROS的外部配置工具,甚至可以对其进行设置,如果我导入并使用其中的模块将是不可能的。

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.

我的 PYTHONHOME 实际上是空的。如果我仅添加建议的编码部分,则会发生与以前相同的错误- PyInitialize CodecRegistryError 等。

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.

再次-如果我从bash启动脚本,该脚本可以正常工作。

Again - the script works without any issues if I start it from bash. It fails ONLY when I run it from PyDev.

推荐答案

我只是尝试了一下,并且它起作用了-我的 Popen()看起来像这样: [ python2.7, / usr / local / bin / rosversion, -d] 并且有效!

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!

由于访问GitHub的问题,我实际上无法访问 rosversion 的源代码,尽管我视而不见通过查看 rospkg 导入失败的错误来得出结论。

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.

不同rospack rosversion 是Python脚本。虽然在bash shell中调用它不会导致问题,但似乎在子进程中运行它需要我实际在该子进程中调用Python解释器并传递绝对路径(我可以使用哪个rosversion )。

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).

这篇关于从Eclipse内部运行Python脚本时的PyDev混乱编码设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆