2011-05-25 48 views
1

在MacBook Pro上使用Mac OS X 10.6.7版本,我有一個簡單的Python腳本 'test.py':Python的子進程失敗,這取決於如何IDLE打開

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

如果我運行此腳本通過使用鼠標在IDLE中打開它,它會崩潰。如果我在終端中通過鍵入'idle'啓動的IDLE運行相同的腳本,它不會崩潰。這是怎麼回事?

詳細信息:通過右鍵點擊test.py

啓動空閒和IDLE(2.6.6) 「打開」。它只打開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並從「運行」菜單中選擇「運行模塊」。以這種方式運行時,終端窗口可以正常打開。我也將下面的Python Shell的內容與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 ================================ 
>>> 
>>> 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 
>>> 

回答

2

您所看到的行爲差異確實與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並且繼承了PATH10由應用程序環境略有不同。特別是,/etc/paths.d沒有被諮詢,所以/usr/X11/bin不在路上。您應該能夠在兩種情況下看到PATH。對於IDLE.app從Finder推出,你可能會看到類似:

>>> 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票友。

+0

絕對路徑似乎是最簡單的,並解決了這個問題。謝謝。 – George 2011-06-09 14:25:56

1

sys.path是不是與此有關(它是用來導入Python模塊)。 您應該檢查PATH環境變量:os.environ['PATH']

OS X終端可能會安裝一些額外的路徑。