Aquesta és una revisió antiga del document —-
5.1 The configparser module
Introduction to the configparser module
Currently, many popular services provide an API that we can use in our applications. Integration with these services requires authentication using data such as a login and password, or simply an access token.
Each service may require different data for authentication, but one thing is certain – we need to store it somewhere in our application. It's not a good idea to hardcode them directly in the code.
A better solution is to use the configuration file, which will be read by the code. In Python, this is possible thanks to a module called configparser.
The configparser module is available in the Python standard library. To start using it, we need to import the appropriate module:
import configparser
However, before we start reading the configuration data, we must familiarize ourselves with the structure of the file in which they're stored. Are you curious how this is done?
What does the configuration file look like?
The structure of the configuration file is very similar to Microsoft Windows INI files. It consists of sections that are identified by names enclosed in square brackets. The sections contain items consisting of key value pairs. Each pair is separated by a colon : or equals sign =.
What's more, the configuration file may contain comments followed by a semicolon ; or hash #:
[DEFAULT] host = localhost # This is a comment. [mariadb] name = hello user = user password = password [redis] port = 6379 db = 0