5

I am trying to overwrite to environment variables in Python. I can read the value and then write the value and print the updated value. But then if I check the value in command line its still the original value. Why is that?

First, I create the variable

export MYVAR=old_val

My test script myvar.py

#!/usr/bin/env python3
import os
print (os.environ['MYVAR'])
os.environ['MYVAR'] = "new_val"
print (os.environ['MYVAR'])

Outputs

$ ./myvar.py 
old_val
new_val
$ echo $MYVAR
old_val

As you can see, the last line of the output still shows the old_val

RayJ_inSJ
  • 329
  • 2
  • 11
  • Does this answer your question? [set environment variable in python script](https://stackoverflow.com/questions/8365394/set-environment-variable-in-python-script) – mrEvgenX Feb 22 '20 at 16:46
  • Or more likely https://stackoverflow.com/questions/1178094/change-current-process-environments-ld-library-path – mrEvgenX Feb 22 '20 at 16:47
  • Does this answer your question? [Change current process environment's LD\_LIBRARY\_PATH](https://stackoverflow.com/questions/1178094/change-current-process-environments-ld-library-path) – AMC Feb 22 '20 at 16:59
  • @mrEvgenX I saw those links before writing my question. Understanding that the script runs in a separate process (as explained in the answers) makes sense to me now. – RayJ_inSJ Feb 23 '20 at 00:40
  • @RayJ_inSJ what's wrong with the given answers. If you're not happy with them, then explain at least what else you're looking for. – gelonida Feb 27 '20 at 01:00
  • Please consider accepting the answer from @gelonida if that is correct and helped you out. That's how StackOverflow works - it is important that correct answers are accepted so that future visitors can be assured that the answer is correct and it floats to the top of the list of answers for all to see. It also rewards folk who give their time for free to help you out by giving them points - which cost you nothing. Thank you. – Mark Setchell Feb 27 '20 at 09:09

2 Answers2

10

Short version:

The python script changes its environment. However this does not affect the environment of the parent process (The shell)

Long version:

Well this is a well know, but quite confusing problem.

What you have to know is, that there is not the environment, each process has its own environment.

So in your example above the shell (where you type your code) has one environment. When you call ./myvar.py, a copy of the current environment is created and passed to your python script. Your code 'only' changes this copy of the environment. As soon as the python script is finished this copy is destroyed and the shell will see its initial unmodified environment.

This is true for most operating systems (Windows, Linux, MS-DOS, ...)

In other words: No child process can change the environment of the process, that called it.

In bash there is a trick, where you source a script instead of calling it as a process.

However if your python script starts another process (for example /bin/bash), then the child process would see the modified environment.

gelonida
  • 5,327
  • 2
  • 23
  • 41
  • 2
    if you really want to change the environment of the current shell and you want to change only one variable, then following would work. You instead of typing `./myvar.py` you had to type `$(./myvar.py)` and the python script would instead of changing the environment just print out the following text: `export MYVAR=new_val` – gelonida Feb 22 '20 at 17:03
2

You started a new process that changed its environment and exited. That's all really.

You shouldn't expect that to affect the process you started it from (your shell).

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432