我正在使用execnet package允许由不同的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
>“首选项/解释器-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.txt
可在eclipse_output.txt vs. terminal_output.pdf找到文件的FileMerge比较.
有什么提示吗?
谢谢
解决方法:
似乎pydev进行站点自定义,并且特别修改了内容以进行交互/控制台使用(从非常快速的浏览器http://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev/pysrc/pydev_sitecustomize/sitecustomize.py判断).这对execnet介导的过程没有用或不适合.
您可以在调用execnet.makegateway之前尝试“ del os.environ [‘PYTHONPATH’]”,或者,要更加小心,只需删除它的sitecustomize部分.
hth,
霍尔格