Configuration functionality#

The description of config below is a quick guide to the basic structure of configuration settings in Sopel. Consult the config.types module for more specific information about the available kinds of options, and config.core_section for examples of how the different setting types are practically used.


Sopel’s configuration module.

The Config object provides an interface to access Sopel’s configuration file. It exposes the configuration’s sections through its attributes as objects, which in turn expose their directives through their attributes.

For example, this is how to access core.nick on a Config object:

>>> from sopel import config
>>> settings = config.Config('/sopel/config.cfg')
>>> settings.core.nick

The configuration file being:

nick = Sopel
host =
use_ssl = true
port = 6697
owner = dgw
channels =

A section can be represented by a subclass of StaticSection; for example a [spam] section with eggs and bacon can be defined like this:

from sopel import config

class SpamSection(config.types.StaticSection):
    eggs = config.types.ListAttribute('eggs')
    bacon = config.types.ValidatedAttribute('bacon')

The [core] section itself is represented by the CoreSection class, which is a subclass of StaticSection. It is automatically added when the Config object is instantiated; it uses Config.define_section() for that purpose.

New in version 6.0.0.

class sopel.config.Config(filename, validate=True)#

The bot’s configuration.

  • filename (str) – the configuration file to load and use to populate this Config instance

  • validate (bool) – if True, validate values in the [core] section when it is loaded (optional; True by default)

The configuration object will load sections (see ConfigSection) from the file at filename during initialization. Calling save() writes any runtime changes to the loaded settings back to the same file.

Only the [core] section (see CoreSection) is added and made available by default; it is the only section required for Sopel to run. All other sections must be defined later, by the code that needs them, using define_section().

class ConfigSection(name, items, parent)#

Represents a section of the config file.

  • name (str) – name of this section

  • items (iterable of two-item tuples) – key-value pairs

  • parent (Config) – this section’s containing object

Contains all keys in the section as attributes.


Add a new, empty section to the config file.


name (str) – name of the new section


None if successful; False if a section named name already exists


Plugin authors very rarely want or need to use this method.

You will almost always want to define (and optionally validate values within) a section with specific attributes using define_section() and a child class of StaticSection.


The section’s name SHOULD follow snake_case naming rules:

  • use only lowercase letters, digits, and underscore (_)

  • SHOULD NOT start with a digit

Deviations from snake_case can break the following operations:


The config’s base filename, i.e. the filename without the extension.

If the filename is libera.config.cfg, then the basename will be libera.config.

The config’s basename is useful as a component of log file names, for example.

define_section(name, cls_, validate=True)#

Define the available settings in a section.

  • name (str) – name of the new section

  • cls_ (subclass of StaticSection) – class defining the settings within the section

  • validate (bool) – whether to validate the section’s values (optional; defaults to True)


ValueError – if the section name has been defined already with a different cls_

If validate is True, the section’s values will be validated, and an exception (usually ValueError or AttributeError) raised if they are invalid. This is desirable in a plugin’s setup() function, for example, but might not be in the configure() function.


The section’s name SHOULD follow snake_case naming rules:

  • use only lowercase letters, digits, and underscore (_)

  • SHOULD NOT start with a digit

Deviations from snake_case can break the following operations:


The config object’s associated file.


Shortcut to parser.get.


Retrieve all defined static sections of this configuration.


all instances of StaticSection defined for this configuration file

Return type:


When a plugin defines a section (using define_section()), it associates a StaticSection for the section. This method allows to retrieve these instances of StaticSection, and only these.

New in version 7.1.

property homedir#

The config file’s home directory.

If the core.homedir setting is available, that value is used. Otherwise, the default homedir is the directory portion of the Config’s filename.

option(question, default=False)#

Ask the user a “y/n” question.

  • question (str) – the question to ask the user

  • default (bool) – True to show [y] as the default choice; False to show [n] (optional; defaults to False)


the Boolean value corresponding to the user’s choice

Return type:


This will show a “y/n” prompt in the user’s terminal, and return True or False based on the response. question should be phrased as a question, but without a question mark at the end.


The configuration parser object that does the heavy lifting.

See also

Python’s built-in configparser module and its RawConfigParser class.


Write all changes to the config file.


Saving the config file will remove any comments that might have existed, as Python’s configparser ignores them when parsing.

This will become less and less important as we continue to improve Sopel’s tools for making automated changes to config files and eliminate most users’ need to ever manually edit the text, but it’s still worth keeping in mind.

exception sopel.config.ConfigurationError(value)#

Exception type for configuration errors.


value (str) – a description of the error that has occurred

exception sopel.config.ConfigurationNotFound(filename)#

Exception type for use when the configuration file cannot be found.


filename (str) – file path that could not be found


Path to the configuration file that could not be found.