Storing configuration data in a global variable on application startup I've never used a global variable in one of my programs(of which there are few) before, is this a fair use of one?

Currently, I have several classes calling a function to grab the data they require from a .yaml file stored in the user home folder. Each class constructs the full path of where the .yaml file should be using two vars; os.path.expanduser("~") and the default filename. This I feel, is messy.

The code below is a simple example of my actual program:

hub.py(Context manager instantiated on startup by main.py):

class Hub:

    def __init__(self):
        self.filename = "config_data.yaml"
        self.filepath = os.path.join(os.path.expanduser("~"), self.filename)


    def __enter__(self):
        global config_data

        with open(self.filepath) as f:
            config_data = yaml.load(f)

        return self

    def __exit__(self, exc_type, value, traceback):
        pass

with Hub() as hub:
    pass


Example use:

from hub import config_data

class Bot:
    def __init__(self):
        self.pricing_params = config_data["pricing"]
Regardless of whether or not this is a good use of global variables, it is a poor use of a class.

Your hub.py could look like this and function exactly the same (you could still import config_data from it):

import os
import yaml

file_path = os.path.join(os.path.expanduser("~"), "config_data.yaml")
with open(file_path) as f:
    config_data = yaml.load(f)


And now that it is obvious that this does not actually do a lot, I would make this a function and put this in your actual script (or maybe a utils script, if you have many more utilities like that):

file utils.py:

import yaml

def get_config(file_path):
    with open(file_path) as f:
        return yaml.load(f)


file bot.py:

import os
from utils import get_config


FILE_PATH = os.path.join(os.path.expanduser("~"), "config_data.yaml")
CONFIG_DATA = get_config(FILE_PATH)

class Bot:
    def __init__(self):
        self.pricing_params = CONFIG_DATA["pricing"]


if __name__ == "__main__":
    bot = Bot()