Set an environment variable

Steve Holden steve at holdenweb.com
Fri Oct 21 03:40:48 EDT 2005


Christian wrote:
>>The closest thing you can do is that:
>>
>>-myScript.py--------------------------------------
>>print 'export MY_VARIABLE=value'
>>--------------------------------------------------
>>
>>-myScript.sh--------------------------------------
>>python myScript.py > /tmp/chgvars.sh
>>. /tmp/chgvars.sh
>>--------------------------------------------------
> 
> 
> Can I write a .py script that calls a .sh script that executes the 
> export command and then calls another .py script (and how would the 
> first .py script look)?
> 
> That would be much more what is my basic problem.
> 
You can do what you suggest without shell scripting, unless I 
misunderstand your intention: just set the environment variables you 
want your Python script to see and then run it using os.system():

::::::::::::::
one.py
::::::::::::::
import os
os.environ['STEVE'] = "You are the man"
os.system("python two.py")
print "Ran one"
::::::::::::::
two.py
::::::::::::::
import os
print "STEVE is", os.environ['STEVE']
print "Ran two"
[sholden at headrat tmp]$ python one.py
STEVE is You are the man
Ran two
Ran one
[sholden at headrat tmp]$

Hope this helps.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list