17

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?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • For windows I can recommend a module to set variables through registry. Also has CLI app: https://github.com/beliaev-maksim/py_setenv this has good control on user/system level and does not have limit in length as setx – Beliaev Maksim Feb 12 '21 at 19:34

5 Answers5

12

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

jackotonye
  • 3,537
  • 23
  • 31
rantanplan
  • 7,283
  • 1
  • 24
  • 45
  • 1
    Just by curiosity, is there a way to accomplish this on Windows? – Alexis Jul 16 '13 at 09:14
  • @Alexis I'm not very familiar with Windows, but according to http://superuser.com/questions/79612/setting-and-getting-windows-environment-variables-from-the-command-prompt, you could use the `setx` command from inside python. Or you could access the win registry directly with the `_winreg` python module http://docs.python.org/2/library/_winreg.html – rantanplan Jul 16 '13 at 09:47
  • For windows I can recommend a module to set variables through registry. Also has CLI app: https://github.com/beliaev-maksim/py_setenv this has good control on user/system level and does not have limit in length as setx – Beliaev Maksim Feb 12 '21 at 19:33
  • 1
    This answer is not true to the spirit of the question. The requestor asked how to set an environment variable via Python, not how to set the environment variable via Python for the next login, or after `source ~/.bashrc` is run. – Wayne Workman Jun 07 '22 at 04:11
6

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 use export )

import subprocess
if os.name == 'posix':  # if is in linux
    exp = 'export hi2="youAsWell"'
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

pelos
  • 1,744
  • 4
  • 24
  • 34
2

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

1

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
Tall Paul
  • 2,398
  • 3
  • 28
  • 34
0

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

Beliaev Maksim
  • 968
  • 12
  • 21