0

In command line Ubuntu LTS16.04, I have:

IMAGE_FILE="a/b/c"

I want to change the value of this variable with python statements. What I have to do? For instance, I want the variable to be set to null "".

I used os.environ, however after exiting python exit (). Run echo $ IMAGE_FILE is still the old value

Thank you for reading the question!

  • 1
    Possible duplicate of [How to set environment variables in Python](http://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python) – Iguananaut May 20 '17 at 13:10
  • Thanks you, I used os.environ, however after exiting python exit (). ; Run echo $IMAGE_FILE in cmd, it is still the old value – Vũ Thành Long May 20 '17 at 13:32

1 Answers1

2

What you ask for cannot be done the way you want it to. The problem is that Python starts as a subprocess of your shell, and a subprocess is not allowed to modify the environmental variable of its parent.

When the subprocess starts, it inherits the environment of its parent and all changes done will be visible in the subprocess, and in further processes spawned by the first subprocess. But, when it exists, the environment of the parent will still remain as before the first subprocess creation.

JohanL
  • 6,671
  • 1
  • 12
  • 26