在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
>>>
答案 0 :(得分:2)
您所看到的行为差异确实与PATH
环境变量有关。当您通过终端shell启动IDLE时,它会从您的shell环境继承PATH
值。 path_helper(8)
通过查询PATH
中的条目来设置登录shell /etc/paths.d/
的默认值。在OS X 10.6上,包括/usr/X11/bin
,这是xterm所在的位置。但是,当您从Finder启动IDLE时,可以通过双击IDLE应用程序图标或使用IDLE作为默认应用程序打开文件(正如您在测试1中所做的那样),不涉及shell和{{应用程序环境继承的1}}略有不同。特别是,PATH
未被咨询,因此/etc/paths.d
不在路径上。您应该能够在两种情况下查看/usr/X11/bin
。对于从Finder启动的IDLE.app,您可能会看到类似的内容:
PATH
虽然有可能to change the default environment variables for processes launched,但很少有必要或不希望这样做。对于这种情况,最简单的解决方案是提供>>> os.environ['PATH']
'/usr/bin:/bin:/usr/sbin:/sbin'
的绝对路径:
xterm
或者你可以自己修改PATH来获得更好的感受。
答案 1 :(得分:1)
sys.path
在这里不相关(它用于导入python模块)。
您应该检查PATH环境变量:os.environ['PATH']
。
OS X终端可能会安装一些额外的路径。