I am trying to write a python script that sets environment variables based on user input. I can set env variables manually in the terminal by typing the regular export myVar='value'. If i type echo $myVar i get the result 'value'. All good so far.
I have this script:
import os
os.environ['myVar'] = 'value'
If i print the variable inside the python session i see the variable. But if i exit python and just do the same echo $myVar i dont see the variable. Ok so the variable is only part of the current python session, its not actually executed in the terminal so i tried something else:
import os
os.system('export myVar=value')
this also does nothing. I dont get the varialbe in the terminal.
For example if i try this:
import os
os.system('gedit')
i get gedit started. So os.system works. But why it doesnt want to set a variable?
I thought this is a python limitation os i tried bashcript
echo "Setting variable"
export myVar='value'
saved this as a var.sh, made it executable with chmod +x and ran it with ./var.sh Still nothing. There is no variable set. Its strange, if i type the same thing in the terminal directly it works, but not through a bash script. I also saw somewhere that you have to source it first. So i created another bash script like this:
source var.sh
echo $myVar
Still no variable set. What am i missing here?
I am using python3.8.5 on popOS 20.04
Thanks!