So, what i'm trying to do is use the python interpreter as sort of a CLI for my server side python app. However, I'm getting a behavior i didn't expect in the loading of python modules.
I have two python files and a cache file:
Main.py
Config.py
Cache.json
So what happens is that when Main is imported and ran, the main() function imports Configs and then calls a function to initialize Config. When initialized Config loads some global variables from Cache.json depending on the environment i'm running in. Here's a simplified example of the .py 's.
#Main.py
import Config
def main():
"""Initialize Python Application"""
Config.init()
if __name__ == "__main__":
main()
#Config.py
import os
import json
Default_MSSQLPassword = ""
Default_MSSQLUser = ""
Default_OdbccDriver = ""
MDGSQLP01 = ""
def init():
"""docstring"""
with open("Cache.json", "r") as f:
dtGlobalConstants = json.load(f)
Default_MSSQLPassword = dtGlobalConstants["Default_MSSQLPassword"]
Default_MSSQLUser = dtGlobalConstants["Default_MSSQLUser"]
Default_OdbccDriver = dtGlobalConstants["Default_OdbccDriver"]
MDGSQLP01 = dtGlobalConstants["MDGSQLP01"]
Now theoretically if I call the below in the python interpreter:
>>>from Main import main
>>>main()
Config should be imported by main(), persist, and I should be able to print the Cached value of Default_OdbccDriver that was loaded form Cache.json. But instead I get this:
>>>Config.Default_OdbccDriver
>>>''
So clearly Main is importing Config.py, because otherwise i'd get an error when calling property .Default_OdbccDriver . However, even though the value of Default_OdbccDriver is supposed to be "global" (according to python's odd definition of the word) it's value should be static and persist in the import cache.
Anyone know what's going on or how to work around it? In the end, I want main() to initialize some values and expose some methods for working with my app, but this isn't a great start...