1

I am trying to set environment variable using python script. I have mentioned the code below

text.py

   import os
   os.environ['Demo'] = "demo"
   print (os.environ)

when i execute text.py file i can set the environment variable for Demo.In print statement I can able to see while executing the script,But when i check in python shell, it doesn't show that environment variable.I want to show that it as permanently. How to do that.

 $python
 >> import os
 >> os.environ
Python Team
  • 1,143
  • 4
  • 21
  • 50
  • Possible duplicate of [How to set environment variables in Python](https://stackoverflow.com/q/5971312/608639), [Reading and writing environment variables in Python?](https://stackoverflow.com/q/5971635/608639), etc. – jww Feb 25 '19 at 19:36
  • 1
    @jww actually my question is about set environment varibale from script. And then check it in python shell. It doesn't show it.So it's not a duplicate question – Python Team Feb 26 '19 at 06:26

1 Answers1

1

i think you cannot do it , since the python script is a child process and all its environment variable will be delete when your process ended , on other hand you can execute the following command on parent to set values to environment variable so as long parent keep running the values will be save

VAR=$(./myprogram)

child_script1.py

#!/usr/bin/python

print ("Setting Value o Parent")

parent.sh

#!/bin/bash

VAR_SET_BY_PYTHON=$(/usr/bin/python script1.py)

echo ${VAR_SET_BY_PYTHON}

i hope this was informative

Juda Barnes
  • 91
  • 1
  • 3