Python Glossary Русский

all Python 3configparserexception

configparser.NoOptionError

exceptions

Raised when a section has no such option (and [DEFAULT] does not have it either). get() and the typed getint/getboolean throw it.

Syntax

except configparser.NoOptionError as e: e.section, e.option

Most often the option exists but is misspelled or sits in another section. Options are compared case-insensitively, so Port and port are the same. The usual fix is the fallback argument: cfg.getint('db', 'port', fallback=5432); a SectionProxy accessed with brackets raises KeyError in the same situation.

Examples

import configparser
cfg = configparser.ConfigParser()
cfg.read_string("[db]\nPort = 5432\n")
try:
    cfg.get("db", "host")
except configparser.NoOptionError as e:
    print(e)  # → No option 'host' in section: 'db'
print(cfg.getint("db", "PORT"), cfg.get("db", "host", fallback="localhost"))  # → 5432 localhost

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy