Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Get System Environment Variable Linux

I've exposed one system environment variable say KEY1 with value VALUE1 in /etc/profile (I know, I know, it's probably bad)

Now in my shell if I do

$ echo $KEY1
VALUE1

But when I do

$ python -c "import os; print os.environ.get('KEY1')"
None
$

Why might this be the case?

like image 229
Ganesh Satpute Avatar asked Sep 02 '26 06:09

Ganesh Satpute


1 Answers

You might not have exported the variable. You can think of export as a way of making a shell variable public and not private to the shell. Take a look at the export man page.

➜  ~ K=1    
➜  ~ echo $K 
1
➜  ~ python -c "import os; print os.environ.get('K')"   
None
➜  ~ export K=1
➜  ~ python -c "import os; print os.environ.get('K')"
1
like image 176
joydeep bhattacharjee Avatar answered Sep 04 '26 20:09

joydeep bhattacharjee



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!