2011-05-25 3 views
1

Sur un MacBook Pro avec Mac OS X version 10.6.7, j'ai un simple script python 'test.py':Python échoue subprocess, selon la façon dont IDLE est ouvert

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

Si je lance ce script en l'ouvrant dans IDLE en utilisant la souris, il se bloque. Si je lance le même script dans un IDLE que je lance en tapant 'idle' dans un terminal, il ne plante pas. Que se passe-t-il?

Détails:

Lancement IDLE par test.py de clic droit et "ouvrir avec" IDLE (2.6.6). Il ouvre uniquement le Shell Python et IDLE, pas test.py. J'ouvre test.py et sélectionnez "exécuter le module" dans le menu "Exécuter". Collé ci-dessous est le contenu de l'interpréteur de commandes Python. Inclus en bas est le sys.path pour IDLE ouvert de cette manière.

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

Lancez IDLE via une fenêtre de terminal. Ouvrez test.py et sélectionnez "Exécuter le module" dans le menu "Exécuter". Lorsqu'il est exécuté de cette manière, la fenêtre du terminal est ouverte correctement. J'ai collé le contenu du shell Python ci-dessous avec le sys.path également.

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

Répondre

2

La différence de comportement que vous voyez est en effet liée à la variable d'environnement PATH. Lorsque vous démarrez IDLE via un shell de terminal, il hérite de la valeur PATH de votre environnement shell. path_helper(8) définit la valeur par défaut pour un shell de connexion PATH en consultant les entrées dans /etc/paths.d/. Sur OS X 10.6, cela inclut /usr/X11/bin, où se trouve xterm. Cependant, lorsque vous lancez IDLE depuis le Finder, soit en double-cliquant sur l'icône de l'application IDLE ou en ouvrant un fichier utilisant IDLE comme application par défaut (comme dans le test 1), un shell n'est pas impliqué et le PATH hérité par l'environnement de l'application est légèrement différente. En règle générale, /etc/paths.d n'est pas consulté et donc /usr/X11/bin n'est pas sur le chemin. Vous devriez être en mesure de voir cela en regardant PATH dans les deux cas. Pour IDLE.app lancé à partir du Finder, vous verrez probablement quelque chose comme:

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

Bien qu'il soit possible to change the default environment variables for processes launched, il est rarement nécessaire ou souhaitable de le faire. Dans ce cas, la solution la plus simple est de fournir le chemin absolu xterm:

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

Ou vous pourriez obtenir en modifiant colombophile vous PATH.

+0

Le chemin absolu semblait le plus simple et résolu le problème. Merci. – George

1

sys.path est pas pertinent (il est utilisé pour importer des modules de python). Vous devez vérifier la variable d'environnement PATH: os.environ['PATH'].

Il est probable que le terminal OS X installe des chemins supplémentaires.

Questions connexes