Python is Not a Configuration File Format

There is a large thread in reddit about using Python as configuration file format. I want to be clear about that:

DON'T DO THAT, UNLESS YOU HAVE A VERY GOOD REASON.

If you need to ask if it's a good idea, then you don't have a good reason. If you are sure that you have a good reason, then maybe you have a good reason.

There are many reasons for it, but I will explore just two, then offer a suggestion.

Python is read-only, and configuring is not programming.

Sure, it's easy to use python as a config file. You just import the thing, and there you go, all the data is there. But now your configuration syntax is a general purpose language that can do things like pop up dialogs when you parse it.

Your config can now depend on the whole internet, the hardware, the weather, and interactive input. Powerful? Sure. Good idea? Maybe, sometimes. But your application is now not able to configure itself.

If your application wants to store any kind of setting, it won't be able to. So most interactive, desktop apps, just should not use python for this, ever.

But what about non-interactive tools? Well, using python means that other tools can't write to the config file, either, which makes the tool less powerful. The power to have tools use tools is one of the cornerstones of modern computing, and you just cut your app off that ecosystem. Depending on what language the tool uses it may not even be able to parse your config file.

And what happens when someone is told "use this config fragment to achieve X"? Well, odds are, if the recipient has done anything that takes advantage of using python as a config format, then the fragment will not work. It would be like doing copy/paste from random code in github into your own program and expecting it to work.

So, you can't write to it from the app, you can't get configuration tips from the internet, you can't use other tools to modify config files, and other tools have a hard time parsing your files.

Also, it means that to handle the general case of configuring your app, you need a programmer. That is almost certainly overkill. Very few apps need that kind of thing. If your app can only be configured by programmers, you may have failed at making a good app (exceptions exist).

And what's the advice? Well, the advice is "don't do that" and the corollary is "configure using data, not code". use INIs, or XML, or YAML, or JSON, or plain text files, or whatever. But not code.

PS: My latest project, Nikola uses python as a configuration language. I thought I had a good reason. I didn't.

Comments

Comments powered by Disqus