-
-:mod:`ConfigParser` --- Configuration file parser
+:mod:`configparser` --- Configuration file parser
=================================================
-.. module:: ConfigParser
+.. module:: configparser
:synopsis: Configuration file parser.
+
.. moduleauthor:: Ken Manheimer <klm@zope.com>
.. moduleauthor:: Barry Warsaw <bwarsaw@python.org>
.. moduleauthor:: Eric S. Raymond <esr@thyrsus.com>
.. sectionauthor:: Christopher G. Petrilli <petrilli@amber.org>
-
.. index::
pair: .ini; file
pair: configuration; file
load the required file or files using :meth:`readfp` before calling :meth:`read`
for any optional files::
- import ConfigParser, os
+ import configparser, os
- config = ConfigParser.ConfigParser()
+ config = configparser.ConfigParser()
config.readfp(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
An example of writing to a configuration file::
- import ConfigParser
+ import configparser
- config = ConfigParser.RawConfigParser()
+ config = configparser.RawConfigParser()
# When adding sections or items, add them in the reverse order of
# how you want them to be displayed in the actual file.
An example of reading the configuration file again::
- import ConfigParser
+ import configparser
- config = ConfigParser.RawConfigParser()
+ config = configparser.RawConfigParser()
config.read('example.cfg')
# getfloat() raises an exception if the value is not a float
To get interpolation, you will need to use a :class:`ConfigParser` or
:class:`SafeConfigParser`::
- import ConfigParser
+ import configparser
- config = ConfigParser.ConfigParser()
+ config = configparser.ConfigParser()
config.read('example.cfg')
# Set the third, optional argument of get to 1 if you wish to use raw mode.
Defaults are available in all three types of ConfigParsers. They are used in
interpolation if an option used is not defined elsewhere. ::
- import ConfigParser
+ import configparser
# New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
- config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
+ config = configparser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
config.read('example.cfg')
print(config.get('Section1', 'foo')) # -> "Python is fun!"
def opt_move(config, section1, section2, option):
try:
config.set(section2, option, config.get(section1, option, 1))
- except ConfigParser.NoSectionError:
+ except configparser.NoSectionError:
# Create non-existent section
config.add_section(section2)
opt_move(config, section1, section2, option)
.. function:: fileConfig(fname[, defaults])
- Reads the logging configuration from a ConfigParser-format file named *fname*.
- This function can be called several times from an application, allowing an end
- user the ability to select from various pre-canned configurations (if the
- developer provides a mechanism to present the choices and load the chosen
- configuration). Defaults to be passed to ConfigParser can be specified in the
- *defaults* argument.
+ Reads the logging configuration from a :mod:`configparser`\-format file named
+ *fname*. This function can be called several times from an application,
+ allowing an end user the ability to select from various pre-canned
+ configurations (if the developer provides a mechanism to present the choices
+ and load the chosen configuration). Defaults to be passed to the ConfigParser
+ can be specified in the *defaults* argument.
.. function:: listen([port])
Configuration file format
^^^^^^^^^^^^^^^^^^^^^^^^^
-The configuration file format understood by :func:`fileConfig` is based on
-ConfigParser functionality. The file must contain sections called ``[loggers]``,
-``[handlers]`` and ``[formatters]`` which identify by name the entities of each
-type which are defined in the file. For each such entity, there is a separate
-section which identified how that entity is configured. Thus, for a logger named
-``log01`` in the ``[loggers]`` section, the relevant configuration details are
-held in a section ``[logger_log01]``. Similarly, a handler called ``hand01`` in
-the ``[handlers]`` section will have its configuration held in a section called
-``[handler_hand01]``, while a formatter called ``form01`` in the
-``[formatters]`` section will have its configuration specified in a section
-called ``[formatter_form01]``. The root logger configuration must be specified
-in a section called ``[logger_root]``.
+The configuration file format understood by :func:`fileConfig` is
+based on :mod:`configparser` functionality. The file must contain
+sections called ``[loggers]``, ``[handlers]`` and ``[formatters]``
+which identify by name the entities of each type which are defined in
+the file. For each such entity, there is a separate section which
+identified how that entity is configured. Thus, for a logger named
+``log01`` in the ``[loggers]`` section, the relevant configuration
+details are held in a section ``[logger_log01]``. Similarly, a handler
+called ``hand01`` in the ``[handlers]`` section will have its
+configuration held in a section called ``[handler_hand01]``, while a
+formatter called ``form01`` in the ``[formatters]`` section will have
+its configuration specified in a section called
+``[formatter_form01]``. The root logger configuration must be
+specified in a section called ``[logger_root]``.
Examples of these sections in the file are given below. ::
.. seealso::
- Module :mod:`ConfigParser`
+ Module :mod:`configparser`
Parser for configuration files similar to the Windows :file:`.ini` files.
import os
import socket
import platform
-import ConfigParser
+import configparser
import httplib
import base64
import urlparse
def parse_config_files (self, filenames=None):
- from ConfigParser import ConfigParser
+ from configparser import ConfigParser
if filenames is None:
filenames = self.find_config_files()
import sys
from idlelib import macosxSupport
-from ConfigParser import ConfigParser, NoOptionError, NoSectionError
+from configparser import ConfigParser, NoOptionError, NoSectionError
class InvalidConfigType(Exception): pass
class InvalidConfigSet(Exception): pass
rather than a filename, in which case the file-like object will be read
using readfp.
"""
- import ConfigParser
+ import configparser
- cp = ConfigParser.ConfigParser(defaults)
+ cp = configparser.ConfigParser(defaults)
if hasattr(cp, 'readfp') and hasattr(fname, 'readline'):
cp.readfp(fname)
else:
self.check_all("BaseHTTPServer")
self.check_all("CGIHTTPServer")
- self.check_all("ConfigParser")
+ self.check_all("configparser")
self.check_all("Cookie")
self.check_all("Queue")
self.check_all("SimpleHTTPServer")
-import ConfigParser
+import configparser
import io
import unittest
import collections
"remove_option() failed to report non-existance of option"
" that was removed")
- self.assertRaises(ConfigParser.NoSectionError,
+ self.assertRaises(configparser.NoSectionError,
cf.remove_option, 'No Such Section', 'foo')
eq(cf.get('Long Line', 'foo'),
def test_parse_errors(self):
self.newconfig()
- self.parse_error(ConfigParser.ParsingError,
+ self.parse_error(configparser.ParsingError,
"[Foo]\n extra-spaces: splat\n")
- self.parse_error(ConfigParser.ParsingError,
+ self.parse_error(configparser.ParsingError,
"[Foo]\n extra-spaces= splat\n")
- self.parse_error(ConfigParser.ParsingError,
+ self.parse_error(configparser.ParsingError,
"[Foo]\noption-without-value\n")
- self.parse_error(ConfigParser.ParsingError,
+ self.parse_error(configparser.ParsingError,
"[Foo]\n:value-without-option-name\n")
- self.parse_error(ConfigParser.ParsingError,
+ self.parse_error(configparser.ParsingError,
"[Foo]\n=value-without-option-name\n")
- self.parse_error(ConfigParser.MissingSectionHeaderError,
+ self.parse_error(configparser.MissingSectionHeaderError,
"No Section!\n")
def parse_error(self, exc, src):
"new ConfigParser should have no defined sections")
self.failIf(cf.has_section("Foo"),
"new ConfigParser should have no acknowledged sections")
- self.assertRaises(ConfigParser.NoSectionError,
+ self.assertRaises(configparser.NoSectionError,
cf.options, "Foo")
- self.assertRaises(ConfigParser.NoSectionError,
+ self.assertRaises(configparser.NoSectionError,
cf.set, "foo", "bar", "value")
- self.get_error(ConfigParser.NoSectionError, "foo", "bar")
+ self.get_error(configparser.NoSectionError, "foo", "bar")
cf.add_section("foo")
- self.get_error(ConfigParser.NoOptionError, "foo", "bar")
+ self.get_error(configparser.NoOptionError, "foo", "bar")
def get_error(self, exc, section, option):
try:
def test_weird_errors(self):
cf = self.newconfig()
cf.add_section("Foo")
- self.assertRaises(ConfigParser.DuplicateSectionError,
+ self.assertRaises(configparser.DuplicateSectionError,
cf.add_section, "Foo")
def test_write(self):
class ConfigParserTestCase(TestCaseBase):
- config_class = ConfigParser.ConfigParser
+ config_class = configparser.ConfigParser
def test_interpolation(self):
cf = self.get_interpolation_config()
"something with lots of interpolation (9 steps)")
eq(cf.get("Foo", "bar10"),
"something with lots of interpolation (10 steps)")
- self.get_error(ConfigParser.InterpolationDepthError, "Foo", "bar11")
+ self.get_error(configparser.InterpolationDepthError, "Foo", "bar11")
def test_interpolation_missing_value(self):
cf = self.get_interpolation_config()
- e = self.get_error(ConfigParser.InterpolationError,
+ e = self.get_error(configparser.InterpolationError,
"Interpolation Error", "name")
self.assertEqual(e.reference, "reference")
self.assertEqual(e.section, "Interpolation Error")
class RawConfigParserTestCase(TestCaseBase):
- config_class = ConfigParser.RawConfigParser
+ config_class = configparser.RawConfigParser
def test_interpolation(self):
cf = self.get_interpolation_config()
class SafeConfigParserTestCase(ConfigParserTestCase):
- config_class = ConfigParser.SafeConfigParser
+ config_class = configparser.SafeConfigParser
def test_safe_interpolation(self):
# See http://www.python.org/sf/511737
Library
-------
+- The ConfigParser module has been renamed to configparser.
+
- Issue 2865: webbrowser.open() works again in a KDE environment.
- The multifile module has been removed.