Reputation: 1171
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
Upvotes: 1
Views: 2227
Reputation: 146
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
Upvotes: 1