I am developing a program that has a settings window in which I can change various parameters for my program. What is the best way to read/save them in some kind of config file? I know that some software and games use .ini
files or similar system. How can I achieve this in Python?
-
Best way is probably xml. – Apr 13 '13 at 20:11
-
9@enginefree, that's literally never true. – Cairnarvon Apr 13 '13 at 20:12
-
4[configparser](http://docs.python.org/3.3/library/configparser.html) is simple and implemented right there in the standard library. – Gareth Latty Apr 13 '13 at 20:12
-
3You could use python as the language for your settings file. – Elmar Peise Apr 13 '13 at 20:13
-
@ExP Although that always adds the possibility of malicious config files - whether or not it's a good idea varies greatly depending on the situation. – Gareth Latty Apr 13 '13 at 20:15
-
2Quite similar to this question which has some very informative answwers: http://stackoverflow.com/questions/5055042/whats-the-best-practice-using-a-settings-file-in-python?rq=1 – guidot Apr 16 '13 at 07:00
-
1An alternative to `ConfigParser` would be to use the [Voidspace](http://www.voidspace.org.uk/) Python module named [ConfigObj](http://www.voidspace.org.uk/python/modules.shtml#configobj) by Michael Foord. See the [**The Advantages of ConfigObj**](http://www.voidspace.org.uk/python/articles/configobj.shtml#the-advantages-of-configobj) section of an article he wrote titled [**_An Introduction to ConfigObj_**](http://www.voidspace.org.uk/python/articles/configobj.shtml) as to why you might want to do this. – martineau Aug 31 '17 at 14:08
-
Also note the (different) accepted answer—mine—to a [question](https://stackoverflow.com/questions/16696281/how-to-store-application-settings-across-modules) that was marked as a duplicate of this one. – martineau Jan 13 '18 at 21:16
3 Answers
The Python standard library includes the ConfigParser
module, which handles ini-style configuration files for you. It's more than adequate for most uses.

- 25,981
- 9
- 51
- 65
-
5Note that in 3.x, it's been renamed [`configparser`](http://docs.python.org/3.3/library/configparser.html). – Gareth Latty Apr 13 '13 at 20:14
Another popular option for configuration files is JSON - it's a simple notation which has good support from a wide range of languages.
Python has the json
module in the standard library, which makes it very easy.

- 86,389
- 17
- 178
- 183
Since you introduced the term config file in your question, the previous answers concentrated on means for creating plain text files, which also could be manipulated using a standard text editor. Depending on the sort of settings to store this might not be desired, since it requires strict plausibility checks after reading back the config file at the very least. So I add the proposal of the shelves module which is a straight-forward way to make information persistent in files.

- 5,095
- 2
- 25
- 37
-
2It's worth noting that in the vast majority of cases, binary config files are considered a bad idea - you make it much harder for the end user to work with them. – Gareth Latty Apr 16 '13 at 13:48