Is there any reason why os.environ contains all environment variables uppercase on Windows?, I don\'t understand why (only on windows) it doesn\'t load them on using the sam
Because Windows environment variables are case insensitive, but a Python dictionary is case sensitive. By uppercasing all entries, you ensure that you'll always be able to match entries.
Quoting from the Python os.py source code:
elif name in ('os2', 'nt'): # Where Env Var Names Must Be UPPERCASE
# But we store them as upper case
# ...
else: # Where Env Var Names Can Be Mixed Case
Note that the os.environ
object translates all access to uppercase, including searches:
def __setitem__(self, key, item):
putenv(key, item)
self.data[key.upper()] = item
def __getitem__(self, key):
return self.data[key.upper()]
# ...
def has_key(self, key):
return key.upper() in self.data
def __contains__(self, key):
return key.upper() in self.data
def get(self, key, failobj=None):
return self.data.get(key.upper(), failobj)
This means that if a program fails to find os.environ['windir']
, then the value is not set.
If you have to have access to the original values, grab them from the nt
module:
import nt
nt.environ
That's the raw initial dictionary as passed in by the OS, unaltered:
>>> import nt
>>> sorted(nt.environ.keys())
['ALLUSERSPROFILE', 'APPDATA', 'COMPUTERNAME', 'ComSpec', 'CommonProgramFiles', 'CommonProgramFiles(x86)', 'CommonProgramW6432', 'FP_NO_HOST_CHECK', 'HOMEDRIVE', 'HOMEPATH', 'LOCALAPPDATA', 'LOGONSERVER', 'NUMBER_OF_PROCESSORS', 'OS', 'PATHEXT', 'PROCESSOR_ARCHITECTURE', 'PROCESSOR_IDENTIFIER', 'PROCESSOR_LEVEL', 'PROCESSOR_REVISION', 'PROMPT', 'PSModulePath', 'PUBLIC', 'Path', 'ProgramData', 'ProgramFiles', 'ProgramFiles(x86)', 'ProgramW6432', 'SESSIONNAME', 'SSH_AUTH_SOCK', 'SystemDrive', 'SystemRoot', 'TEMP', 'TMP', 'USERDNSDOMAIN', 'USERDOMAIN', 'USERNAME', 'USERPROFILE', 'windir', 'windows_tracing_flags', 'windows_tracing_logfile']