1

I'm developing a python project. My project folder structure looks something like this:

rootfolder
     -- config folder     (has config class which gives config object)
     -- main folder1
           -- sub folders
     -- main folder2
           -- sub folders

I have a main.py in my main folders which need config object. Also there are a lot of modules in my sub folders which needs access to config object. So currently, I'm instantiating config object in my main.py and passing config object to each of my sub modules. Is there a way to make my config object globally available to all modules? Something like environment variables.

I tried something like this in my main code:

config = Config.getConfig() # fetching config object which is of type dict
os.environ['config'] = config

But it does not work since os.environ will only take strings. Thanks in advance for the help!

Kas1
  • 145
  • 1
  • 4
  • 14
  • Does this answer your question? [Python, variable importing between files](https://stackoverflow.com/questions/60838018/python-variable-importing-between-files) – MisterMiyagi Jun 16 '20 at 07:21
  • Hi, Thanks for the link. It looks like that question is not related to my question. – Kas1 Jun 16 '20 at 07:27
  • Okay. Be aware that my advice would have been the same approach as in the question: Create a *module* that represents the configuration, and make that available to submodules via importing. – MisterMiyagi Jun 16 '20 at 07:28
  • Hi, to elaborate more my config module is connecting to a DB. I thought that importing the config module in each of my sub modules is a bad idea since we open multiple connections. – Kas1 Jun 16 '20 at 16:22
  • Even if you import a module multiple times, it is *executed* only once. – MisterMiyagi Jun 16 '20 at 17:07

0 Answers0