[Tutor] Changing (Unix) environment for python shell/popen() commands

"Jörg Wölke" lumbricus at gmx.net
Sun Jan 23 20:37:35 CET 2005


> Hey all.

Hello!
 
> I'm unfortunately stuck using python 1.5.2, primarily on Linux 
> currently, and have done the usual searching (on list, active state, 
> google), without luck.
> 
> I've got to shell out from my python code to execute a command, but 
> _must_ set the environment at the same time (or prior to execution).  I 
> say must, because in this case the obvious answer of modifying the 
> command to be called or wrapping it with a shell script to set the 
> environment won't work/isn't going to happen.
> 
> I saw some comments about setting os.environ[<some shell var>], but 
> didn't seem to be seeing this work in subsequent calls using popen2().

What is the error?

> Does anyone have a working piece of code setting some env variable 
> successfully prior to(or along with) calling popen2() (or friends).
> Also, is there any way to get the called programs return code via 
> popen() (again, under python 1.5.2)?
 

>>> os.environ['murx']="hello world"
>>> os.system("echo $murx")
>>> x=os.popen("echo $murx").readline()
>>> x
'hello world\n'
>>> os.system("man bash")

> I know this has been asked often enough, but once again, it seems I'm 
> left wanting for _good_ documentation under python (ie compared to man 
> pages, DevStudio Win32 docs, Linux info, Java docs, etc), including the 
> half dozen python books I own  (great for some things, but no reference 
> whatsoever to other things that I'd think would warrant some coverage)..
> 
> Thanks,
> 
> Scott
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Wir sind jetzt ein Imperium und wir schaffen uns
unsere eigene Realität. Wir sind die Akteure der 
Geschichte, und Ihnen, Ihnen allen bleibt nichts,
als die Realität zu studieren, die wir geschaffen haben.
        -- Karl Rove zu Ron Suskind (NYT)

Sparen beginnt mit GMX DSL: http://www.gmx.net/de/go/dsl


More information about the Tutor mailing list