Linux os.environ 不显示一些变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7110587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:45:06  来源:igfitidea点击:

os.environ doesn't show some variables

pythonlinux

提问by pbanka

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!

有人有任何想法吗?我以前从未见过这个!

采纳答案by SingleNegationElimination

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 exportthe variable:

您还(可能)需要export变量:

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