简体   繁体   中英

Set Environment Variable permanently even after execution of script in linux from python script

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM