Set an environment variable

Grant Edwards grante at visi.com
Fri Oct 21 15:19:04 EDT 2005


On 2005-10-21, Mike Meyer <mwm at mired.org> 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
>>>> --------------------------------------------------
>>
>> Bullshit.  Are people being intentionally misleading??
>
> No. Are you being intentionally - never mind.

Well, yes, probably.

>> And even Google knows the correct answer
>>
>> http://www.google.com/search?hl=en&lr=&q=python+set+environment+variable
>>
>> Follow the first hit.

My bad.  I got links mixed up -- it wasn't the first one, it
was this one:

http://www.faqts.com/knowledge_base/view.phtml/aid/3298

There are two almost-equivalent tirival answers:

  os.environment['foo'] = 'bar'

  os.putenv('foo','bar')
  
I don't get why people seem to be obfuscating things with
multiple layers of shells or writing shell commands to to a
file and executing them.

> Maybe the results order has changed since you looked?

No, I mixed them up.

My point: the OP wanted to know how to export an environment
variable to a child process.  Either of the lines of code above
will do that, so what's with all the shellular shenanigans?

-- 
Grant Edwards                   grante             Yow!  Youth of today! Join
                                  at               me in a mass rally
                               visi.com            for traditional mental
                                                   attitudes!



More information about the Python-list mailing list