HMan06
HMan06

Reputation: 775

Handling Python Configuration File Across Entire Project

I am creating a piece of code that will have multiple files that need to reference a single config. The reason is that our IT department will use puppet to manage this config file in case any changes are required in the future. We don't want to do a release to change the configuration. I've seen a few projects that have multiple configs in different places but I really do not like this idea and I'd prefer to have a single source. My thought was to create a specific config.py file that can be called anywhere in my code that will ask for the user to input the location of the file.

Is this a good way or is there a better way to do this?

import configparser

class Config(object):

    def __init__(self,conf):
        self._cfg = configparser.ConfigParser()
        self._cfg.read(conf)


    def get_conf_value(self,property):
        if property not in self._cfg.sections:
            return None
        return self._cfg.sections[property]

If so, if I have a Main.py while, what's the best way to have the scheduler pass the config location and then reference it across all of my files in my Python Package?

Upvotes: 1

Views: 793

Answers (1)

Neeraj Agarwal
Neeraj Agarwal

Reputation: 1059

You can create a config.json file in json format. You can read the contents at startup using the json.load function from the json library.

Upvotes: 1

Related Questions