How to set a environment variable in the current shell with Python?
Question: I want to set an environment variable with a
[Python](http://en.wikipedia.org/wiki/Python_%28programming_language%29)
script, influencing the shell I am starting the script in. Here is what I mean

    
    
    python -c "import os;os.system('export TESTW=1')"
    

But the command

    
    
    echo ${TESTW}
    

returns nothing. Also with the expression

    
    
    python -c "import os;os.environ['TEST']='1'"
    

it does not work.

Is there another way to do this in the direct sense? Or is it better to write
the variables in a file which I execute from 'outside' of the Python script?


Answer: You can influence environment via:
[putenv](https://docs.python.org/2/library/os.html#os.putenv) BUT it will not
influence the caller environment, only environment of forked children. It's
really much better to setup environment before launching the python script.

I may propose such variant. You create a bash script and a python script. In
bash script you call the python script with params. One param - one env
variable. Eg:

    
    
    #!/bin/bash
    
    export TESTV1=$(python you_program.py testv1)
    export TESTV2=$(python you_program.py testv2)
    

and `you_program.py testv1` returns value just for one env variable.

