Python获取系统环境变量Linux [英] Python Get System Environment Variable Linux

查看:477
本文介绍了Python获取系统环境变量Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在/etc/profile中公开了一个值为VALUE1的系统环境变量KEY1(我知道,我知道这可能很不好)

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

但是当我这样做

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

为什么会这样?

推荐答案

您可能尚未导出变量.您可以将export看作是使Shell变量成为公共变量而不是私有给Shell的一种方法.看看导出手册页.

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

这篇关于Python获取系统环境变量Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆