Eclipse 通过 execnet 使用多个 Python 解释器
我使用 execnet 包 来允许不同解释的 Python 脚本之间进行通信Python 解释器。
以下代码 (test_execnet.py):
import execnet
for python_version in ('python', 'python3'):
try:
gw = execnet.makegateway("popen//python="+python_version)
ch = gw.remote_exec('channel.send(1/3)')
res = ch.receive()
print(python_version, ': ', res, sep ="")
except:
print('problems with ', python_version)
在命令行终端中完美运行,显示以下输出:
$ python3 test_execnet.py
python: 0
python3: 0.333333333333
但是,如果我尝试从 Eclipse IDE 中运行相同的代码,则会收到以下错误:
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 4, in <module>
File "<string>", line 2, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages/execnet/gateway_base.py", line 8, in <module>
import sys, os, weakref
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/os.py", line 380, in <module>
from _abcoll import MutableMapping # Can't use collections (bootstrap)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/_abcoll.py", line 54
class Hashable(metaclass=ABCMeta):
^
SyntaxError: invalid syntax
problems with python
problems with python3
注意:
- Eclipse 版本: 3.6.0
- 为项目配置的
- PyDev解释器:python3 “Preferences/Interpreter - Python”的Python解释器:
- python (/usr/bin/python)
- python3(/Library/Frameworks/Python.Framework/Versions/3.1/Resources/Python.app/Contents/MacOS/Python
编辑:
我编写了一段代码来显示 os.environ
像这样:
for python_version in ('python', 'python3'):
try:
import os
for item in os.environ:
print(item, '= ', os.environ[item])
except:
print('problems with ', python_version)
我得到以下输出:
文件合并比较可在 eclipse_output.txt 与terminal_output.pdf。
="http://www.educoelho.com/files/eclipse_output.txt_vs._terminal_output.pdf" rel= " 谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎 pydev 进行了站点自定义,特别是修改了交互式/控制台使用的内容(从 http://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev/pysrc/pydev_sitecustomize/sitecustomize.py< /a>)。这对于 execnet 介导的进程没有用处或不适合。
您可以在调用 execnet.makegateway 之前尝试“del os.environ['PYTHONPATH']”,或者更小心,只需删除其中的 sitecustomize 部分。
哈,
霍尔格
seems like pydev does site-customizations and particularly modifies things for interactive/console usage (judging from a very quick skim of http://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev/pysrc/pydev_sitecustomize/sitecustomize.py ). This is not useful or fitting for execnet-mediated processes.
You could try to "del os.environ['PYTHONPATH']" before you invoke execnet.makegateway, or, to be more careful, just delete the sitecustomize part of it.
hth,
holger
我见过当 python 无法找到它的地标时。这表明存在 PYTHONHOME 问题。
查看 http://docs.python.org/using/cmdline.html# envvar-PYTHONHOME 也许 Eclipse 正在搞砸你的环境。
编辑:
查看你的环境转储,看起来 eclipse 肯定会弄乱 PYTHONPATH,这将导致你的子 python 进程无法正常工作。
基本上,您在这里所做的是 eclipse 启动一个 python v2 实例,其中 PYTHONPATH 指向 python v2 目录。然后生成一个 python v3 进程,该进程尝试从 python v2 目录加载其地标...
你需要找到一种方法让 Eclipse 不会弄乱 PYTHONPATH。我不确定 Eclipse 这样做是想做什么,但当你想生成新的 python 进程时,它肯定不是朋友。
I have seen that when python was unable to find its landmark. Which that indicates there is a PYTHONHOME problem.
Check out http://docs.python.org/using/cmdline.html#envvar-PYTHONHOME maybe eclipse is screwing your environment up.
Edit:
Looked at your env dumps, looks like eclipse is definitely messing with PYTHONPATH, which will cause your child python processes to not work correctly.
Basically what you have going on here is eclipse starts a python v2 instance with a PYTHONPATH pointing to the python v2 directories. Then you spawn a python v3 process which tries to load its landmark from the python v2 directories...
You need to find a way to have eclipse not mess with the PYTHONPATH. I am not sure what eclipse is trying to do by doing that, but it is certainly no friend when you want to spawn new python processes.