Is it possible to set an environment variable from Python permanently?

If what you want is to make your environment variables persist accross sessions, you could

For unix

do what we do when in bash shell. Append you environment variables inside the ~/.bashrc.

import os
with open(os.path.expanduser("~/.bashrc"), "a") as outfile:
    # 'a' stands for "append"  
    outfile.write("export MYVAR=MYVALUE")

or for Windows:

setx /M MYVAR "MYVALUE"

in a *.bat that is in Startup in Program Files

Leave a Comment