开发者

os.environ doesn't show some variables

开发者 https://www.devze.com 2023-03-28 20:48 出处:网络
I have an environment variable that I set (on Centos 6) using profile.d, as follows: [bankap@tnt-integration-test ~]$ cat /etc/profile.d/tnt.sh

I have an environment variable that I set (on Centos 6) using profile.d, as follows:

[bankap@tnt-integration-test ~]$ cat /etc/profile.d/tnt.sh
TNT_SERVER_URL=http://tnt-integration-test:8000/

and when I log in, I see the variable:

[bankap@tnt-integration-test ~]$ echo $TNT_SERVER_URL
http://tnt-integration-test:8000/

But when I access the thing with Python, the environment variable doesn't show up!

[bankap@tnt-integration-test ~]$ python -c 'import os;os.environ.get("TNT_SERVER_URL")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'TNT_SERVER_URL' is not defined

I've even trie开发者_运维问答d using the ctypes library with the same results:

>>> os.getenv('TNT_SERVER_URL')
>>> from ctypes import CDLL, c_char_p
>>> getenv = CDLL('libc.so.6').getenv
>>> getenv('TNT_SERVER_URL')
0
>>>

But other variables come through just fine...

os.environ {'SSH_ASKPASS': '/usr/libexec/openssh/gnome-ssh-askpass', 'LESSOPEN': '|/usr/bin/lesspipe.sh %s', 'SSH_CLIENT': '139.126.176.137 56535 22', 'SELINUX_USE_CURRENT_RANGE': '', 'LOGNAME': 'bankap', 'USER': 'bankap', 'QTDIR': '/usr/lib64/qt-3.3', 'PATH': '/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/bankap/bin',

Anybody have any ideas? I've never seen this before!


You have a quoting problem:

change

python -c 'import os;os.environ.get('TNT_SERVER_URL')'

into

python -c 'import os;os.environ.get("TNT_SERVER_URL")'
                                    ^              ^

You also (probably) need to export the variable:

export TNT_SERVER_URL; python -c 'import os;os.environ.get("TNT_SERVER_URL")'
0

精彩评论

暂无评论...
验证码 换一张
取 消