Is it ever polite to put code in a python configuration file?

One of my favorite features about python is that you can write configuration files in python that are very simple to read and understand. If you put a few boundaries on yourself, you can be pretty confident that non-pythonistas will know exactly what you mean and will be perfectly capable of reconfiguring your program.

My question is, what exactly are those boundaries? My own personal heuristic was

  1. Avoid flow control. No functions, loops, or conditionals. Those wouldn't be in a text config file and people aren't expecting to have understand them. In general, it probably shouldn't matter the order in which your statements execute.
  2. Stick to literal assignments. Methods and functions called on objects are harder to think through. Anything implicit is going to be a mess. If there's something complicated that has to happen with your parameters, change how they're interpreted.
  3. Language keywords and error handling are right out.

I guess I ask this because I came across a situation with my Django config file where it seems to be useful to break these rules. I happen to like it, but I feel a little guilty. Basically, my project is deployed through svn checkouts to a couple different servers that won't all be configured the same (some will share a database, some won't, for example). So, I throw a hook at the end:

    from settings_overrides import *
    LOCALIZED = True
except ImportError:
    LOCALIZED = False

where settings_overrides is on the python path but outside the working copy. What do you think, either about this example, or about python config boundaries in general?

Asked by: Maddie720 | Posted: 06-12-2021

Answer 1

There is a Django wiki page, which addresses exactly the thing you're asking.

Do not reinvent the wheel. Use configparser and INI files. Python files are to easy to break by someone, who doesn't know Python.

Answered by: Sienna579 | Posted: 07-01-2022

Answer 2

Your heuristics are good. Rules are made so that boundaries are set and only broken when it's obviously a vastly better solution than the alternate.

Still, I can't help but wonder that the site checking code should be in the parser, and an additional configuration item added that selects which option should be taken.

I don't think that in this case the alternative is so bad that breaking the rules makes sense...


Answered by: Emma339 | Posted: 07-01-2022

Answer 3

I think it's a pain vs pleasure argument.

It's not wrong to put code in a Python config file because it's all valid Python, but it does mean you could confuse a user who comes in to reconfigure an app. If you're that worried about it, rope it off with comments explaining roughly what it does and that the user shouldn't edit it, rather edit the file.

As for your example, that's nigh on essential for developers to test then deploy their apps. Definitely more pleasure than pain. But you should really do this instead:


    from settings_overrides import *
except ImportError:

And in your file:


... If nothing but to make it clear what that file does.. What you're doing there splits overrides into two places.

Answered by: Elise144 | Posted: 07-01-2022

Answer 4

As a general practice, see the other answers on the page; it all depends. Specifically for Django, however, I see nothing fundamentally wrong with writing code in the file... after all, the settings file IS code :-)

The Django docs on settings themselves say:

A settings file is just a Python module with module-level variables.

And give the example:

assign settings dynamically using normal Python syntax. For example:
MY_SETTING = [str(i) for i in range(30)]

Answered by: Aldus720 | Posted: 07-01-2022

Answer 5

Settings as code is also a security risk. You import your "config", but in reality you are executing whatever code is in that file. Put config in files that you parse first and you can reject nonsensical or malicious values, even if it is more work for you. I blogged about this in December 2008.

Answered by: Caroline766 | Posted: 07-01-2022

Similar questions

Configuration file with list of key-value pairs in python

I have a python script that analyzes a set of error messages and checks for each message if it matches a certain pattern (regular expression) in order to group these messages. For example "file x does not exist" and "file y does not exist" would match "file .* does not exist" and be accounted as two occurrences of "file not found" category. As the number of patterns and categories is growing, I'd like to put thes...

python - django application configuration

I'm dying to get started Django but I'm really struggling with the initial setup. I have Python/MySql/Apache2.2/mod_python installed. Now I'm trying to create a proper directory structure and then update Django and Apache docs respectively. Specifically the location tag in the latter. Django and Python are based on simplicity but this is one huge oversight from the Django folks to not provide more guidanc...

python - Py2Exe - "The application configuration is incorrect."

I've compiled my Python program using Py2Exe, and on the client's computer we've satisfied all the dependencies using dependency walker, but we still get "The application configuration is incorrect. Reinstalling the application may correct the problem." I'm also using wxPython. The client does not have administrator access. Any ideas?

python - What is the best approach to implement configuration app with Django?

I need to program kind of configuration registry for Django-based application. Requirements: Most likely param_name : param_value structure Editable via admin interface Has to work with syncdb. How to deal with a situation in which other apps depend on configuration model and the model itself has not been initialized yet in DB? Let's say I would like to hav...

python - SQLAlchemy - Mapper configuration and declarative base

I am writing a multimedia archive database backend and I want to use joined table inheritance. I am using Python with SQLAlchemy with the declarative extension. The table holding the media record is as follows: _Base = declarative_base() class Record(_Base): __tablename__ = 'records' item_id = Column(String(M_ITEM_ID), ForeignKey('')) storage_id = Column(String(M_STORAGE_ID), ForeignKe...

oop - reading a configuration information only once in Python

I'm using the ConfigParser to read the configuration information stored in a file. I'm able to read the content and use it across other modules in the project. I'm not sure if the configuration file is read every time I call config.get(parameters). How can I make sure that the configuration information is read only once and rest of the time its read from the cache.

command line arguments - Python - configuration options, how to input/handle?

When your application takes a few (~ 5) configuration parameters, and the application is going to be used by non-technology users (i.e. KISS), how do you usually handle reading configuration options, and then passing around the parameters between objects/functions (multiple modules)? Options examples: input and output directories/file names...

configuration - Where to store secret keys and password in Python

I have a small Python program, which uses a Google Maps API secret key. I'm getting ready to check-in my code, and I don't want to include the secret key in SVN. In the canonical PHP app you put secret keys, database passwords, and other app specific config in LocalSettings.php. Is there a similar file/location which Python programmers expect to find and modify?

What is a good configuration file library for c thats not xml (preferably has python bindings)?

Closed. This question does not meet Stack Overflow guid...

python - How to setup mod_python configuration variables?

I'm running a Python server with mod_python, and I've run into some issues with configuration variables. This is actually two questions rolled into one, because I think they are highly related: I need a way to configure variables that will be available in Python while running. I currently just have a module that sets some name-value pairs that I import into other modules, but I was reading up on

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python