0

I tried both

os.environ['TemporaryVar'] = "Apple"

and

os.putenv("TemporaryVar", "Apple")

but after running the python script, when i run the command env or set in the command line, i do not see variable as

TemporaryVar=Apple

I also have a sleep(50) in the python script so that the script does not terminate while i am checking for set or env in command line.

What i want is to write a python script that adds a variable to the environment variable. (I should be able to see it when i type env)

Chetan
  • 469
  • 3
  • 12
  • 27
  • 1
    You can't set the environment variable for a parent process (i.e., the shell that invoked your Python process). See https://stackoverflow.com/a/38982936 – chrisaycock Apr 18 '18 at 17:15

1 Answers1

3

This is not possible.

Environment variables are defined in the scope of the job that sets it. That's why when you define an environment variable in one terminal session ,you won't see it in other sessions. The same thing is happening with your script. A job runs your python script but you are trying to read variables from another terminal session(another job), so you can't see them. Depending on your goal, you have to find a workaround.

atakanyenel
  • 1,367
  • 1
  • 15
  • 20