【问题标题】:Python subprocess fails, depending on how IDLE is openedPython 子进程失败,取决于 IDLE 的打开方式
【发布时间】:2011-05-25 19:50:11
【问题描述】:

在装有 Mac OS X 版本 10.6.7 的 MacBook Pro 上,我有一个简单的 python 脚本“test.py”:

import subprocess
subprocess.Popen(['xterm'])

如果我通过使用鼠标在 IDLE 中打开它来运行此脚本,它会崩溃。如果我在通过在终端中键入“idle”启动的 IDLE 中运行相同的脚本,它不会崩溃。怎么回事?

详情:

通过右键单击 test.py 并“打开”IDLE (2.6.6) 来启动 IDLE。它只打开 Python Shell 和 IDLE,而不是 test.py。我打开 test.py 并从“运行”菜单中选择“运行模块”。下面粘贴的是来自 Python Shell 的内容。底部包含以这种方式打开的 IDLE 的 sys.path。

Python 2.6.6 (r266:84292, May 11 2011, 21:44:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 2.6.6      
>>> ================================ RESTART ================================
>>> 

Traceback (most recent call last):
  File "/Users/georgepatterson/test.py", line 2, in <module>
    subprocess.Popen(['xterm'])
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 623, in __init__
    errread, errwrite)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 1141, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

>>> import sys
>>> for p in sys.path: print p

/Users/georgepatterson
/Users/georgepatterson/Documents
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyObjC
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info
>>> 

通过终端窗口启动 IDLE。打开 test.py 并从“运行”菜单中选择“运行模块”。以这种方式运行时,终端窗口会正确打开。我也用 sys.path 粘贴了下面 Python Shell 的内容。

Python 2.6.6 (r266:84292, May 11 2011, 21:44:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 2.6.6      
>>> ================================ RESTART ================================
>>> 
>>> import sys
>>> for p in sys.path: print p

/Users/georgepatterson
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyObjC
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info
>>>

【问题讨论】:

    标签: python macos subprocess python-idle


    【解决方案1】:

    您看到的行为差异确实与PATH 环境变量有关。当您通过终端 shell 启动 IDLE 时,它会从您的 shell 环境中继承 PATH 值。 path_helper(8) 通过查阅 /etc/paths.d/ 中的条目来设置登录 shell PATH 的默认值。在 OS X 10.6 上,这包括 /usr/X11/bin,这是 xterm 所在的位置。但是,当您从 Finder 启动 IDLE 时,通过双击 IDLE 应用程序图标或使用 IDLE 作为默认应用程序打开文件(如您在测试 1 中所做的那样),不涉及 shell 并且 @987654328 @被app环境继承的略有不同。特别是,/etc/paths.d 未被咨询,因此/usr/X11/bin 不在路径上。在这两种情况下,您都应该能够通过查看PATH 看到这一点。对于从 Finder 启动的 IDLE.app,您可能会看到如下内容:

    >>> os.environ['PATH']
    '/usr/bin:/bin:/usr/sbin:/sbin'
    

    虽然to change the default environment variables for processes launched 是可能的,但很少有必要或不希望这样做。对于这种情况,最简单的解决方案是提供xterm 的绝对路径:

    import subprocess
    subprocess.Popen(['/usr/X11/bin/xterm'])
    

    或者您可以通过自己修改 PATH 来获得更好的体验。

    【讨论】:

    • 绝对路径似乎最简单,解决了问题。谢谢。
    【解决方案2】:

    sys.path 在这里不相关(它用于导入 python 模块)。 您应该检查 PATH 环境变量:os.environ['PATH']

    OS X 终端可能安装了一些额外的路径。

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 2010-10-26
      • 2011-04-26
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      相关资源
      最近更新 更多