Search code examples
pythonpython-3.xconfigconfigparser

AttributeError: type object 'ConfigParser' has no attribute 'Error'


from configparser import ConfigParser

configfile = 'config.ini'

cparser = ConfigParser()
try:
    # assigns values from config
except ConfigParser.Error as e:
    print(e)

This code was working a day ago, i dont know if i changed something and it messed things up, but it no longer works

Traceback (most recent call last):
  File "C:\Users\k\ping_off-1\v-ups-daemon.py", line 60, in <module>
    except ConfigParser.Error as e:
AttributeError: type object 'ConfigParser' has no attribute 'Error'

Solution

  • The Error exception is in the base configparser module, not in the configparser.ConfigParser class. Adjust your code to the following:

    import configparser as cfg
    
    configfile = 'config.ini'
    
    cparser = cfg.ConfigParser()
    try:
        # assigns values from config
    except cfg.Error as e:
        print(e)