Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables set from python not visible in shell script

I have a python script which sets environment variables. I am running this python script from a shell script and the expectation is to read the value of environment variables set by the python script.

#Python script - x.py#

import os
os.environ['FRUIT'] = 'APPLE'

#Shell script - x.sh#

python -c "import x"
echo $FRUIT

But that does not seem to be working. Nothing gets printed by the shell script. Can any one please explain why it is so and how to fix this?

like image 620
Sushant Gupta Avatar asked Dec 09 '25 07:12

Sushant Gupta


1 Answers

Environment variables set in your Python script will be visible only from processes launched by the Python script, because that's how environment variables work. You can't chnage environment variables in someone else's process. There is no way to "fix" it because it's that way by design.

like image 135
kindall Avatar answered Dec 11 '25 21:12

kindall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!