environment variable issue

Diez B. Roggisch deets at nospam.web.de
Mon Sep 15 14:54:42 EDT 2008


Michel Leunen schrieb:
> Hi,
> 
> Could someone explain me what I'm doing wrong here? I'm trying to 
> retrieve the value of an environment variable in Ubuntu 8.04 like this:
> 
>  >>> import os
>  >>> os.environ['USER']
> 'michel'
> 
> This works but this doesn't:
> 
>  >>> os.environ['HOSTNAME']
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
>     raise KeyError(key)
> KeyError: 'HOSTNAME'
> 
> Why is it working in the first case but not in the second one. I must be 
> missing something but it seems I'm not able to figure it out.

Maybe hostname is just missing? What does os.environ.keys() yield?

On *my* ubuntu, it gives

 >>> sorted(os.environ.keys())
['HOME', 'JAVA_HOME', 'LANG', 'LANGUAGE', 'LOGNAME', 'MAIL', 'PATH', 
'PWD', 'PYTHONSTARTUP', 'SHELL', 'SHLVL', 'SSH_CLIENT', 
'SSH_CONNECTION', 'SSH_TTY', 'TERM', 'USER', '_']
 >>>

No HOSTNAME ...


Diez



More information about the Python-list mailing list