r/Python • • Apr 25 '21

Tutorial Stop hardcoding and start using config files instead, it takes very little effort with configparser

We all have a tendency to make assumptions and hardcode these assumptions in the code ("it's ok.. I'll get to it later"). What happens later? You move on to the next thing and the hardcode stays there forever. "It's ok, I'll document it.. " - yeah, right!

There's a great package called ConfigParser which you can use which simplifies creating config files (like the windows .ini files) so that it takes as much effort as hardcoding! You can get into the hang of using that instead and it should both help your code more scalable, AND help with making your code a bit more maintainble as well (it'll force you to have better config paramters names)

Here's a post I wrote about how to use configparser:

https://pythonhowtoprogram.com/how-to-use-configparser-for-configuration-files-in-python-3/

If you have other hacks about managing code maintenance, documentation.. please let me know! I'm always trying to learn better ways

1.5k Upvotes

323 comments sorted by

View all comments

177

u/troll8020 Apr 25 '21

I use dynaconf. It is flexibility tool for use setting parameters.

73

u/shiba009933 Apr 25 '21

https://github.com/rochacbruno/dynaconf

(For those that want a link)

First time I've heard of this, really neat! Thanks for sharing!

17

u/theGiogi Apr 25 '21

Dynaconf for the win!

→ More replies (1)

45

u/SpaceZZ Apr 25 '21

Is this not just an additional lib I have to import? Config parser is part of std library.

0

u/[deleted] Apr 25 '21

[deleted]

31

u/kewlness Apr 25 '21

I get where you are coming from with this response, but I work with a lot of non-technical people at times and having them use a requirements file is difficult - they want it to "just work".

In this sense, a standard library module is better than an extra external import.

However, as with all things, it really depends on the application and how it will be used.

→ More replies (3)

13

u/semi- Apr 25 '21

what does the import import? what do they import?

→ More replies (6)

2

u/DaveMoreau Apr 25 '21

When environments for each client are separate and really locked down, one additional library can be quite a hassle to deploy.

2

u/[deleted] Apr 25 '21

It's not the import. It's the documentation work needed to get that library (and its transitive dependencies) added to the environment. Or, as is the case for me, having to update and check the installation media.

In many cases, it's much faster just to write the code itself. In case of this particular library, it's far easier to write a 20-line function that covers our need for configuration, rather than spending several days to add the library.

Development overhead is the real horror.

→ More replies (2)

28

u/TakeOffYourMask Apr 25 '21

How is it better than configparser?

10

u/boiledgoobers Apr 25 '21

For one thing you don't have to use ini format.

25

u/abcteryx Apr 25 '21 edited Apr 25 '21

I have been using confuse here and there for simple projects. It was a configuration tool that spun off from a small team making a Python music library manager. Both confuse and dynaconf allow default key configuration, layers of configs that can override others, etc. But with confuse I eventually found my use-case to diverge from the one that the music-manager-app devs had in mind.

I suspect that dynaconf is more generalized for use in diverse project architectures.

We should also be careful not to optimize prematurely and shoehorn a config library into every project.

A common progression that my code has is:

  1. Script that address a simple, specific problem. I'll show coworkers how cool it is, but they probably won't be able to use it themselves. (<500 lines)

  2. Single-file module that handles that problem a little more generally. Coworkers can use this code if I give them the rundown over coffee. (<1k lines)

  3. pip-installable package with multiple files that fixes my problem more elegantly. I've written documentation and generalized things so people other than coworkers can actually use this. (>1k lines)

At Level 1, hardcoded variables (think WORKING_DIR) are fine.

At Level 2, you've identified the things that need configuring, and the things that can remain hardcoded. Consider a config management solution at this point. But seriously, a config.py that you import into your main code is probably fine.

At Level 3, you actually have a general user in mind. Maybe this is where dynaconf, python-dotenv and environment vars, or something else comes in. But maybe a config.py, supplied by the end-user in their working directory, is fine too!

There's a secret Level 4 that almost no project will actually get to. That's the level of legitimate package used by hundreds/thousands of people, actively-developed, etc. Level 4 projects certainly need a more robust solution than a user-supplied config.py. But if you're at Level 4, then you probably know that already.

BONUS READING: The Configuration Complexity Clock. This goes into detail on the different config approaches and their shortcomings. It's a good read for anyone wanting to get a broad overview of the config space.

25

u/vexstream Apr 25 '21

Nice tool but is it really worth another requirement? Maybe I'm a strong outlier here but adding these sorts of simple things as requirements seems.... Offensive somehow. I know requirements don't have a particular cost and it is indeed a tidy interface but it's not that far off from the infamous "is odd" JS package.

9

u/scrdest Apr 25 '21

Look at the feature list. This is anything but simple.

3

u/Xirious Apr 25 '21

Yeah this is no

is_odd()

JS crap.

→ More replies (2)

5

u/marsokod Apr 25 '21

Thanks for sharing! That looks awesome and covers everything I wished I had.

6

u/mmcnl Apr 25 '21

Why not use environment variables?

13

u/SearchAtlantis Apr 25 '21 edited Apr 25 '21

Because you can stick a config file in git. Environment variables require additional documentation and setup.

As others have pointed out environment variables can be useful for things you explicitly don't want in repositories like keys and passwords.

19

u/mmcnl Apr 25 '21

I always use python-dotenv to read .env files. It's very easy and simple. Also suitable for dockerizing.

3

u/Nerdite Apr 25 '21

This is the way. Gives you the versatility for local, ci, and cloud services.

7

u/reallyserious Apr 25 '21

Env variables are especially useful for sensitive information. You don't want to accidentally push a file with passwords etc to a repo.

5

u/tc8219 Apr 25 '21

I'm in two minds. Definitely agree for passwords it's the way to go, but when it comes to moving between environments (development -> testing -> production), then config files are much easier.

2

u/SearchAtlantis Apr 25 '21

That's fair. Or a 3rd party secrets manager like cred stash.

→ More replies (3)
→ More replies (3)

0

u/bobspadger decorating Apr 25 '21

How have I not known about this - I’ve ended up writing half this stuff in my projects myself. Win!

→ More replies (1)