corazza
corazza

Reputation: 32374

How to implement a settings file in a Python program

A "settings file" would be a file where things like "background color", "speed of execution", "number of x's" are defined. Currently, I implemented it as a single setting.py file, which I import in the beginning. Someone told me I should make it a settings.ini file instead, but I don't see why! Care to clarify, what is the optimal option?

Upvotes: 3

Views: 1791

Answers (4)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83686

For Python use stock libraries:

YAML style configuration files:

http://www.yaml.org/start.html

http://pypi.python.org/pypi/PyYAML/

(Used e.g. Google App Engine)

INI: http://docs.python.org/library/configparser.html

Don't use XML for hand-edited config files.

Upvotes: 1

Vikas
Vikas

Reputation: 8948

There are a few reasons why separating out config files from main codebase is a good idea. Of course it depends on your use case and you should evaluate against your usecase.

  1. Configuration can be managed by end user, who do not understand programming languages. It makes more sense to factor out configuration and use a simple ini file which uses simple key-value pairs for config parameters.

  2. Configuration varies based on the installation environment. Your code runs on multiple environment and they all use different configuration. It is very easy to maintain such cases by having separate config files and same source code installed on those environments.

  3. There are package managers that knows what is a config file and what is a source file. They are intelligent to not override any changed config on version upgrade etc. So you do not have to worry about resetting config parameters after version upgrade of package. For example you ship your product with a default config file. User fine tuned few parameters. You shipped another version of the package. User should not expect a config reset after version upgrade.

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409442

One problem with having a settings file being a Python module is that it can contain code that will be executed when you import it. This may allow malicious code to be inserted into your program.

Upvotes: 2

ninjagecko
ninjagecko

Reputation: 91149

There is no optimal solution; it is a matter of preference.*

Normally, settings do not need to be expressed in a Turing-complete language: they're often just a bunch of flags and options, sometimes strings and numbers, etc. An argument for having a settings.py file (though very unorthodox) would be if the end-user was expected to write code to generate very esoteric configurations (e.g. maps for a game). This would then be fairly similar to shell script .bashrc-style files.

But again, in 99.9% of programs, the settings are often just a bunch of flags and options, sometimes strings and numbers, etc. It's fine to store them as JSON or XML. It also makes it easy to perform reflection on your settings: for example, automatically listing them in a tree manner, or automatically creating a GUI out of the descriptions.

(Also it may be a (unlikely?) security issue if you allow people to inject code by modifying the settings file.)

*edit: no pun intended...

Upvotes: 5

Related Questions