简体   繁体   English

os.environ没有显示一些变量

[英]os.environ doesn't show some variables

I have an environment variable that I set (on Centos 6) using profile.d, as follows: 我有一个环境变量,我使用profile.d设置(在Centos 6上),如下所示:

[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! 但是当我使用Python访问该东西时,环境变量不会显示出来!

[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: 我甚至尝试使用ctypes库,结果相同:

>>> 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', 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变量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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