Popen Question

moogyd moogyd at yahoo.co.uk
Thu Nov 4 10:06:24 EDT 2010


Hi,
I usually use csh for my simulation control scripts, but these scripts
are becoming more complex, so I plan to use python for the next
project.
To this end, I am looking at subprocess.Popen() to actually call the
simulations, and have a very basic question which is demonstrated
below.

[sde:staff at lbux03 ~]$ python
Python 2.6 (r26:66714, Feb 21 2009, 02:16:04)
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, subprocess
>>> os.environ['MYVAR'] = "myval"
>>> p = subprocess.Popen(['echo', '$MYVAR'],shell=True)
>>>
>>> p = subprocess.Popen(['echo', '$MYVAR'])
>>> $MYVAR

>>> p = subprocess.Popen('echo $MYVAR',shell=True)
>>> myval

>>> p = subprocess.Popen('echo $MYVAR')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/subprocess.py", line 595, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1106, in
_execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I am not really sure I understand these results.
1) No idea what is going on
2) As (1). What isn't myval printed out (rather than $MYVAR)
3) Works as I wanted it to
4) Why do I need shell=True ?
The documentation isn't very clear to me (it seems you need to
understand the underlying system calls).

Can anyone explain (or provide link) for this behaviour in simple
English?
Thanks,
Steven



More information about the Python-list mailing list