ConfigParser в Python — как его использовать?
Файлы конфигурации используются как пользователями, так и программистами. Как правило, их используют для хранения настроек вашего приложения, или операционной системы. Библиотека в ядре Python включает в себя модуль, под названием configparser, который вы можете использовать для создания и работы с файлами конфигурации. Что ж, давайте выделим несколько минут на то, чтобы узнать, как это работает.
Совместимость с Python 3
Из за нововведений в стандарте PEP 8 модуль ConfigParser в Python 3 был переименован в configparser. Возможные ошибки:
ImportError: no module named ConfigParser
Решение проблемы совместимости
Создание файла
Создание файла config при помощи configparser невероятно просто. Давайте напишем небольшой код, чтобы посмотреть, как это работает:
Данный код создает файл config с одной секцией, под названием Settings, которая будет содержать наши опции: font, font_size, font_style и font_info. Обратите внимание на то, что в Python 3 нам нужно указать, что мы пишем файл в режиме write-only, или “w”. А в Python 2.7, мы использовали “wb” для написания в бинарном режиме.
Как читать, обновлять и удалять опции
Теперь мы готовы к тому, что бы научиться чтению файла config, обновлять его опции и даже удалять их. В нашем случае учиться будет намного проще, если мы попробуем на практике написать какой-нибудь код. Просто добавьте следующую функцию в код, который вы писали ранее.
Этот код сначала проверяет, существует ли файл config в принципе. Если его нет, то он использует созданную нами ранее функцию createConfig, чтобы создать файл. Далее мы создаем объект ConfigParser и указываем путь к файлу config для чтения. Чтобы прочесть опцию в вашем config файле, мы вызываем метод нашего объекта ConfigParser, указываем ему наименование секции и опции.
Есть вопросы по Python?
На нашем форуме вы можете задать любой вопрос и получить ответ от всего нашего сообщества!
Telegram Чат & Канал
Вступите в наш дружный чат по Python и начните общение с единомышленниками! Станьте частью большого сообщества!
Паблик VK
Одно из самых больших сообществ по Python в социальной сети ВК. Видео уроки и книги для вас!
Это вернет значение параметра. Если вы хотите изменить значение опции, вам нужно использовать метод set, в котором вы указываете название секции, опции, и новое значение. Наконец, мы можем использовать метод remove_option, чтобы удалить опцию. В нашем примере мы изменили значение font_size, и задали ему размер 12, затем мы удалили опцию font_style. После этого мы записали наши изменения на диск. Этот пример на на столько хорош, давайте упростим наш код. Для этого мы разделим наш код на на несколько функции:
Этот пример выглядит более организованно, по сравнению с первым. Я зашел так далеко, что назвал функции соответственно стандартам PEP8. Каждая функция должна объяснять сама себя и выполнять лишь одну задачу. Вместо того, чтобы помещать всю логику в одну единственную функцию, мы разделяем её на несколько функций, после чего демонстрируем их функционал в конце оператора if. Теперь вы можете импортировать модуль и использовать по назначению. Обратите внимание на то, что в этом примере есть сложная секция, так что вам, возможно, захочется усовершенствовать этот пример в дальнейшем, чтобы сделать его более универсальным.
Как использовать интерполяцию
Модуль configparser также подразумевает возможность интерполяции, что значит, что вы можете использовать существующие опции, для создания другой опции. Мы на самом деле это делали с опцией font_info, чьи параметры основаны на опциях font и font_size. Мы можем изменить интерполированное значение при помощи словаря Python. Давайте уделим несколько минут, и взглянем на оба случая.
How To Work With Config Files In Python
Working with INI-files in Python using the built-in configparser module
Introduction¶
For every developer the day comes, when he or she is working on large and consequently complex projects. To maintain the software, configuration files can be very beneficial and time-saving. Instead of changing the source code itself, «only» configuration files need to be adjusted or exchanged to access a different API endpoint, update the base URL, or similar things.
While there are various ways to support configuration files in your software including JSON, YAML, and plain-text files, this article aims to give you an introduction to the configparser module from the standard library.
Note: This article is based on Python 3.9.0 (CPython). The source code can be found on GitHub.
File Structure¶
Before jumping into the code, let’s have a look at how an actual configuration file can look like.
In the example at hand, we have a configuration file called config.ini, which consists of three sections. Each section consists of a section title, which is encapsulated within square brackets, and a list of key-value pairs. Notice that comments are supported, too. Everything after a # (number sign, hash) or ; (semicolon) will be ignored.
While the moderator and admin sections are simply collections of key-value pairs, the DEFAULT section (first section) is somewhat special. It contains default values if one of the other sections does not provide a value for a certain key. Consequently, if you try to access a value in one of the other sections but the key is not present, the parser returns the value from the default section (if present) instead of raising a KeyError . More on that later.
Let’s finalise the story of our configuration file. For this scenario, we manage a user’s page access through this configuration file. Therefore, the default section represents the permissions for a normal user, whereas the moderator and admin sections contain the permissions for moderators and administrators respectively.
Accessing the File’s Content¶
The ConfigParser object is the main configuration parser and the main object of the configparser module. You could implement your own configuration parser using the mapping protocol, but let’s stick to the ConfigParser in this article.
Note: There is also a legacy API available with explicit get() and set() methods. However, it is highly preferred to stick to the ConfigParser class or to implement a custom parser based on the mapping protocol.
While there are a bunch of parameters, which ConfigParser accepts, we will stick to the default values in this article. However, if you are looking for more customisation, make sure to check out the respective part of the documentation [ 1 ].
Let’s create a new file in our working directory alongside the config.ini file called parser_playground.py. First of all, we import the ConfigParser class from the configparser module and create an instance of that class.
Our config object does not contain any information, yet. To change that, we need to read the config.ini file first. This can be done by calling the read() method of the ConfigParser instance (here config ).
Notice, that the read() method also accepts a list of path-like objects [ 2 ], which it will read from. After reading the configuration file, we can start exploring how to access information stored in it. Let’s start by having a look at how to handle sections. First, we want to list all available sections. This can be achieved by using the ConfigParser ‘s sections() method:
Furthermore, we can explicitly check, whether a certain section exists by using the parser’s has_section() method:
Note: The default section is neither listed when calling the sections() method nor is it acknowledged by the has_section() method.
Next, we want to access individual values. But before accessing a specific value using its identifier, we can list all available options of one section using the options() method and supplying the section name as an argument:
Additionally, we can utilise the has_option() method to check whether a given section includes a certain option:
To access the values of a section, you can use the parser’s get() method and supply a section name and an option name. The values will always be strings if present. If you need them in another format, consider using the respective getboolean() , getint() , and getfloat() methods. They will try to parse the strings to the desired data type.
To conclude this section, Mapping Protocol Access needs to be mentioned. This generic name means that values can be accessed as if we were dealing with a dictionary. Namely, we can use the config[«section»][«option»] notation to access a certain value or even check if a certain option is present in a section:
Modifying Information¶
Next, let’s have a look at how to add or change information and write it back to the configuration file. Again, we start with sections. To add a section, we can use the ConfigParser ‘s add_section() method. It accepts a section name as a string and adds the respective section to the parser. Supplying a different data type results in a TypeError . If the section already exists, a DuplicateSectionError is raised. Trying to name the section default results in a ValueError .
To delete a section, simply use the remove_section() method.
Python’s ConfigParser object provides similar methods for manipulating options. For instance, the set() method can be invoked to not only add new options to a section but update existing options as well. Likewise, if you want to delete a certain option entirely, use the parser’s remove_option() method.
After manipulating the configuration, we can write it back to the same or a different file as follows:
Notice, that the write() method accepts a file object [ 3 ], which is opened in text mode (accepting string, not bytes). This closes the section about manipulating information of the configuration read earlier from config.ini.
Interpolation¶
Last but not least, let’s have a look at something making ConfigParser superior to Python’s json module (at least in my opinion): Interpolation. Interpolation means that values can be pre-processed before they are returned by calls of some get() method. The configparser module provides two interpolation classes: BasicInterpolation and ExtendedInterpolation . The first one only allows reusing options from the same section within the configuration file and its syntax is not as pretty as the one from the latter class. That is why we keep things simple at this point and only have a look at the ExtendedInterpolation class.
The following snippet shows you a configuration file making use of extended interpolation syntax.
In essence, the first section defines the path to the root directory. This path is used as a prefix for the second option, the path to the downloads directory. In the second section, we have an option app_dir , which reuses the definition of the downloads directory from the section paths .
To realise that, we tell the ConfigParser to use the ExtendedInterpolation as interpolation type when we instantiate the parser:
Note: An actual interpolation instance needs to be passed to the ConfigParser , so do not forget the parentheses () .
If we now print the value for the app_dir option of the destinations section, we get an interpolated string.
Summary¶
Congratulations, you have made it through the article! You not only learnt how to access values from files using the INI-structure, but how to manipulate and extend them, as well. Furthermore, you learnt about the configparser‘s interpolation capabilities and how to utilise them for your needs.
I hope you enjoyed reading the article. Feel free to share it with your friends and colleagues! Do you have feedback? I am eager to hear it! You can contact me via the contact form or other resources listed in the contact section.
If you have not already, consider following me on Twitter, where I am @DahlitzF, or subscribing to my newsletter! Stay curious and keep coding!
Configuration files in Python
Most interesting programs need some kind of configuration:
- Content Management Systems like WordPress blogs, WikiMedia and Joomla need to store the information where the database server is (the hostname) and how to login (username and password)
- Proprietary software might need to store if the software was registered already (the serial key)
- Scientific software could store the path to BLAS libraries
For very simple tasks you might choose to write these configuration variables directly into the source code. But this is a bad idea when you upload the code to GitHub.
I will explain some alternatives I got to know for Python.
Python Configuration File
The simplest way to write configuration files is to simply write a separate file that contains Python code. You might want to call it something like databaseconfig.py . Then you could add the line *config.py to your .gitignore file to avoid uploading it accidentally.
A configuration file could look like this:
Within the actual code, you can use it like this:
The way you include the configuration might feel very convenient at a first glance, but imagine what happens when you get more configuration variables. You definitely need to provide an example configuration file. And it is hard to resist the temptation to include code within the configuration file.
JSON is short for JavaScript Object Notation. It is widespread and thus has good support for many programming languages.
The configuration might look like this:
You can read it like this:
Writing JSON files is also easy. Just build up the dictionary and use
YAML is a configuration file format. Wikipedia says:
YAML (rhymes with camel) is a human-readable data serialization format that takes concepts from programming languages such as C, Perl, and Python, and ideas from XML and the data format of electronic mail (RFC 2822). YAML was first proposed by Clark Evans in 2001, who designed it together with Ingy döt Net and Oren Ben-Kiki. It is available for several programming languages.
The file itself might look like this:
You can read it like this:
There is a yaml.dump method, so you can write the configuration the same way. Just build up a dictionary.
YAML is used by the Blender project.
Resources
INI files look like this:
ConfigParser
Basic example
The file can be loaded and used like this:
As you can see, you can use a standard data format that is easy to read and write. Methods like getboolean and getint allow you to get the datatype instead of a simple string.
Writing configuration
Seems not to be used at all for configuration files by the Python community. However, parsing / writing XML is easy and there are plenty of possibilities to do so with Python. One is BeautifulSoup:
where the config.xml might look like this:
File Endings
File Endings give the user and the system an indicator about the content of a file. Reasonable file endings for configuration files are
- *config.py for Python files
- *.yaml or *.yml if the configuration is done in YAML format
- *.json for configuration files written in JSON format
- *.cfg or *.conf to indicate that it is a configuration file
- *.ini for «initialization» are quite widespread (see Wiki)
That said, I think I prefer *.conf . I think it is a choice that users understand.
But you might also consider that *.ini might get opened by standard in a text editor. For the other options, users might get asked which program they want to use.
How to read and write INI file with Python3?
I need to read, write and create an INI file with Python3.
FILE.INI
Python File:
UPDATED FILE.INI
9 Answers 9
This can be something to start with:
Here’s a complete read, update and write example.
Input file, test.ini
Output file, test_update.ini
The original input file remains untouched.
Python’s standard library might be helpful in this case.
The standard ConfigParser normally requires access via config[‘section_name’][‘key’] , which is no fun. A little modification can deliver attribute access:
AttrDict is a class derived from dict which allows access via both dictionary keys and attribute access: that means a.x is a[‘x’]
We can use this class in ConfigParser :
and now we get application.ini with:
contents in my backup_settings.ini file
python code for reading
for writing or updating
![]()
ConfigObj is a good alternative to ConfigParser which offers a lot more flexibility: