Roman
Roman

Reputation: 131158

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

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

os.environ['HOME']

However, I cannot set the environment variables:

os.environ['BLA'] = "FOO"

It works in the current session of the program but when I python program is finished, I do not see that it changed (or set) values of the environment variables. Is there a way to do it from Python?

Upvotes: 18

Views: 31820

Answers (6)

pelos
pelos

Reputation: 1876

if you want to do it and set them up forever into a user account you can use setx but if you want as global you can use setx /M but for that you might need elevation, (i am using windows as example, (for linux you can see the other post where they store the value into the bash file )

import subprocess
if os.name == 'nt':  # if is in windows
    exp = 'setx hi2 "youAsWell"'
subprocess.Popen(exp, shell=True).wait()

after running that you can go to the environments and see how they been added into my user environments

enter image description here

Upvotes: 5

aarsht7
aarsht7

Reputation: 1

A good Solution for people looking to apply this solution for windows can be the following

For user specific,

from os import path
with open(path.join(path.expanduser('~'),"AppData","Roaming","Microsoft","Windows"
                    ,"Start Menu","Programs","Startup","script.ps1"), 'w') as fp:
        fp.write('[Environment]::SetEnvironmentVariable("THIS_IS_NAME", "THIS_IS_VALUE", "User") ')

Ref to the command that is being excecuted for set environment. lmk if there are any typos ;)

Upvotes: 0

Beliaev Maksim
Beliaev Maksim

Reputation: 1589

you can use py-setenv to do that job. It will access windows registry and do a change for you, install via python -m pip install py-setenv

Upvotes: 0

Sudhanshu Dixit
Sudhanshu Dixit

Reputation: 31

@rantanplan's answer is correct. For Unix however, if you wish to set multiple environment variables i suggest you add a \n at the end of outfile line as following:

outfile.write("export MYVAR=MYVALUE\n")

So the code looks like the following:

import os
with open(os.path.expanduser("~/.bashrc"), "a") as outfile:
    # 'a' stands for "append"  
    outfile.write("export MYVAR1=MYVALUE1\n")
    outfile.write("export MYVAR2=MYVALUE2\n")

This will prevent appending "export" word at the end of the value of the environment variable.

Upvotes: 3

rantanplan
rantanplan

Reputation: 7450

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

Upvotes: 12

Tall Paul
Tall Paul

Reputation: 2450

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

Upvotes: 1

Related Questions