Eclipse 通过 execnet 使用多个 Python 解释器

发布于 2024-09-10 00:51:29 字数 2637 浏览 10 评论 0 原文

我使用 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= " 谢谢

I'm using the execnet package to allow communication between Python scripts interpreted by different Python interpreters.

The following code (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)

Runs perfectly in the command-line Terminal, showing the following output:

$ python3 test_execnet.py 
python: 0
python3: 0.333333333333

However, if I try to run the same code from within the Eclipse IDE, I get the following error:

'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

NOTE:

  • Eclipse Version: 3.6.0
  • PyDev Interpreter configured for the project: python3
  • "Preferences/Interpreter - Python"'s Python Interpreters:
    • python (/usr/bin/python)
    • python3 (/Library/Frameworks/Python.Framework/Versions/3.1/Resources/Python.app/Contents/MacOS/Python

EDIT:

I write a code to show the os.environ like this:

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)

I got the following outputs:

A FileMerge comparison of the files can be found at eclipse_output.txt vs. terminal_output.pdf.

Any hints?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放赐 2024-09-17 00:51:29

似乎 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

过期情话 2024-09-17 00:51:29
'import site' failed; use -v for traceback

我见过当 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 进程时,它肯定不是朋友。

'import site' failed; use -v for traceback

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文