python os.getenv()不能正常获取到环境变量是什么原因?,pythonos.getenv,我在/etc/profi


我在/etc/profile中写一个变量内容
echo 能取到这个值,但python的os.getenv(),不能取到,这是为咋的?

演示内容:

[root@vultrvpn ~]# vim /etc/profile[root@vultrvpn ~]# source /etc/profile[root@vultrvpn ~]# echo $ABC_TESTISNEILSI[root@vultrvpn ~]# pythonPython 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import os>>> os.getenv("ABC_TEST")>>> print os.getenv("ABC_TEST")None

os.getenv("ABC_TEST")得到的值为空的。

我不知道你的 ABC_TEST 是怎么赋值的,不使用export 的话,只是在当前的shell里面赋值哦!

$ aaa="test_aaa"$ export bbb="test_bbb"$ echo $aaatest_aaa$ echo $bbbtest_bbb$ pythonPython 2.7.10 (default, Jul 30 2016, 19:40:32)[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import os>>> os.getenv("aaa")>>> os.getenv("bbb")'test_bbb'>>> print os.getenv("aaa")None>>> print os.getenv("bbb")test_bbb

大概是因为PYTHON执行的环境和shell环境不是同一个东西。

编橙之家文章,

评论关闭