0

I'm trying to create an environment variable in Ubuntu by Python.

Here is what I've done so far:

import os

os.environ['NEW_ENV_VAR'] = '1'
print(os.environ['NEW_ENV_VAR'])

Out:

1

Apparently, I can write and read this variable on this same code, but I couldn't read it on its OS or another code:

$ echo $NEW_ENV_VAR


Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • 2
    A child process cannot affect the environment of its parent. – chepner Oct 05 '19 at 13:54
  • 4
    Possible duplicate of [How to set environment variables of parent shell in Python?](https://stackoverflow.com/q/263005/608639), [Setting environment variables of parent shell in Python?](https://stackoverflow.com/q/35780715/608639), etc. – jww Oct 05 '19 at 14:07
  • Like @chepner said what you're trying to do is impossible in a UNIX like OS such as Linux. Environment variables are not global. They are private to each process. By default a new process inherits the env vars of its parent but the parent process can also give the new process a custom set of env vars. Your python process cannot modify its parent process environment. – Kurtis Rader Oct 05 '19 at 20:56

0 Answers0