Re: 2 inter-related questions

Tim Peters (tim@ksr.com)
Sun, 05 Jul 92 14:19:02 -0400

>>Ok, since my python script requires argv[1], I have to call it from
>>the shell, right ? There is not way from the python interpreter to go:
>>
>>>>> set shellProgramArgs = 'test2.c'
>>>> exec 'compilify'
>>
>>So that Python will issue compilify test2.c to the shell right ?

I'm not sure what you're after here either, but suspect the following
might do the trick. Here's file "compilify":

#!/lang/users/tim/bin/python

import sys
print 'I think my name is', sys.argv[0]
print 'I think my arguments are', sys.argv[1:]

And here's an interpreter session:

>>> import os
>>> os.system('compilify test2.c')
I think my name is ./compilify
I think my arguments are ['test2.c']
0
>>>

Note that "0" was printed by the interpreter; it's the exit status of
the "system" call. You can suppress that by assigning to a dummy vrbl,
like:

>>> dummy = os.system('compilify test2.c')
I think my name is ./compilify
I think my arguments are ['test2.c']
>>>

I doubt you really want the `os.exec' function, because that will
replace the interpreter with the thing being exec'ed.

As Guido mentioned, this will also do the trick (so long as the thing
you want to run from the interpreter is a Python script; 'system' and
'exec' can execute anything):

>>> import sys
>>> sys.argv = ['blah', 'test2.c']
>>> execfile('compilify')
I think my name is blah
I think my arguments are ['test2.c']
>>>

>>os.popen execs its argument based on its own shell , not the one with
>>my PATH environmental variable . As a result , it cant find things
>>that are own my path that are not on the default PATH . Besides moving
>>the executable to the current directory or another place on the
>>default PATH, is there a workaround ?

>[guido]
>Sounds like a problem in your C library -- on my system, popen
>definitely sees my $PATH. Are you sure you are exporting $PATH from
>your shell?

I don't think I understood the original question -- an example would
sure help!

But no problems with $PATH here either. I'll just add that if the thing
being popen'd is a Python script, it won't find $PATH in sys.path, but
in os.environ['PATH']. I've seen Python beginners panic over that
before (they look at sys.path & figure their $PATH has been destroyed).

>>Also : all of this is going into a FAQ sheet .
>Is this a threat? :-)

no-but-this-is<grin>-ly y'rs - tim

Tim Peters Kendall Square Research Corp
tim@ksr.com, ksr!tim@uunet.uu.net