Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most pythonic method for specifying a configuration file?

Tags:

python

I'm writing a daemon script in Python and would like to be able to specify settings in a file that the program expects to read (like .conf files). The standard library has configparser and xdrlib file formats, but neither seems pythonic; the former being a Microsoft standard and the latter a Sun Microsystems standard.

I could write my own, but I'd rather stick to a well-known and documented standard, than reinvent the wheel.

like image 909
Andy Avatar asked Mar 18 '13 20:03

Andy


People also ask

Which formats can be used to define a configuration file?

Configuration files have largely adopted a serialization format, such as XML, YAML and JSON, to represent complex data structures in a way that is easily stored and parsed. The resulting filenames can reflect the format. For example, a configuration file in YAML format may appear such as myapplication_configuration.

What is the best configuration language?

Simple configuration languages, such as JSON, work for many applications, but when you need proper validation, schema, and namespace support, XML is often best.

How do you configure a file in Python?

Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.

What is config () in Python?

A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.


1 Answers

Unless you have any particularly complex needs, there's nothing wrong with using configparser. It's a simple, flat file format that's easily understood and familiar to many people. If that really rubs you the wrong way, there's always the option of using JSON or YAML config but those are somewhat more heavyweight formats.

A third popular option is to just use Python for your configuration file. This gives you more flexibility but also increases the odds of introducing strange bugs by 'just' modifying your config file.

like image 168
Sean McSomething Avatar answered Sep 30 '22 14:09

Sean McSomething