title: Python ConfigParser KeyError:
body: <p>I am following an example to read a config file from the following <a href="https://wiki.python.org/moin/ConfigParserExamples" rel="nofollow">https://wiki.python.org/moin/ConfigParserExamples</a>
  But I get a keyError and can't figure out why. It is reading the files and I can even print the sections. I think I am doing something really stupid. Any help greatly appreciated.</p>
  
  <p>Here is the code...</p>
  
  <pre><code>#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  
  import ConfigParser
  import logging
  
  config_default=ConfigParser.ConfigParser()
  
  class Setting(object):
  
      def get_setting(self, section, my_setting):
  
          default_setting = self.default_section_map(section)[my_setting]
  
          return default_setting
  
      def default_section_map(self,section):
          dict_default = {}
  
          config_default.read('setting.cfg')
          sec=config_default.sections()
  
  
          options_default = config_default.options(section)
  
          logging.info('options_default: {0}'.format(options_default))
  
  
          for option in options_default:
              try:
                  dict_default[option] = config_default.get(section, option)              
  
                  if dict_default[option] == -1:
                      print("skip: %s" % option)
              except:
                  print("exception on %s!" % option)
                  dict_default[option] = None
  
              return dict_default
  
  
          return complete_path
  
  if __name__ == '__main__':
  
      conf=Setting()
  
      host=conf.get_setting('mainstuff','name')
      #host=conf.setting
  
      print 'host setting is :' + host
  </code></pre>
  
  <p>My config file is named setting.cfg and looks like this...</p>
  
  <pre><code>[mainstuff]
  name              = test1
  domain              = test2
  
  [othersection]
  database_ismaster   = no
  database_master     = test3
  database_default    = test4
  
  [mysql]
  port                = 311111
  user                = someuser
  passwd              = somecrazylongpassword
  
  [api]
  port                = 1111
  </code></pre>
  
  <p>And the Error is this...</p>
  
  <blockquote>
    <p>exception on domain! Traceback (most recent call last):   File
    "./t.py", line 51, in 
        host=conf.get_setting('mainstuff','name')   File "./t.py", line 14, in get_setting
        default_setting = self.default_section_map(section)[my_setting] KeyError: 'name'</p>
  </blockquote>