Set an environment variable

Eric Brunel eric_brunel at despammed.com
Thu Oct 20 10:15:01 EDT 2005


On 20 Oct 2005 01:58:44 -0700, the_crazy88 <python at gentoolinux.demon.nl> wrote:

> Just use
> os.system("export PYTHONPATH = %s" %("your_pythonpath"))

... except it won't work: os.system will execute the command in a new process, so the environment variable change will only be visible in *this* process. Since you don't do anything else, the environment variable change will never be seen by anyone.

As for the OP's question, the short answer is "you can't": the Python interpreter will always be executed in a different process than the calling shell, and environment variable changes *never* impact the calling process on Unix.

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
--------------------------------------------------

This is quite ugly: you write the shell commands changing the environment variables to a file, then "source" this file in the calling shell. But this is probably the best way to do what you want.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"



More information about the Python-list mailing list