environment variable issue

Diez B. Roggisch deets at nospam.web.de
Mon Sep 15 20:54:42 CEST 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())



More information about the Python-list mailing list