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

后端 未结 2 1981
既然无缘
既然无缘 2020-12-10 04:23

Using os module I can get the values of the environment variables. For example:

os.environ[\'HOME\']

However, I cannot set the

相关标签:
2条回答
  • 2020-12-10 04:46

    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

    0 讨论(0)
  • 2020-12-10 04:58

    I am not sure. You can return the variable and set it that way. To do this print it.

    (python program)

    ...
    print foo
    

    (bash)

    set -- $(python test.py)
    foo=$1
    
    0 讨论(0)
提交回复
热议问题