简体   繁体   中英

os.environ doesn't show some variables

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 tried 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")'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM