Reputation: 2304
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...
Upvotes: 2
Views: 1872
Reputation: 695
It looks like you are using config as if it were a class, but not defining it as one. if you want a config object, you can define it instead like this:
#Config.py
import os
import json
class Config:
def __init__(self):
"""docstring"""
dtGlobalConstants = {}
with open("Cache.json", "r") as f:
dtGlobalConstants = json.load(f)
Default_MSSQLPassword = dtGlobalConstants.get("Default_MSSQLPassword", "")
Default_MSSQLUser = dtGlobalConstants.get("Default_MSSQLUser", "")
Default_OdbccDriver = dtGlobalConstants.get("Default_OdbccDriver", "")
MDGSQLP01 = dtGlobalConstants.get("MDGSQLP01", "")
Now, when you first instantiate a config object, like config = Config()
, the config
object will have all those variables.
Note also that the dict.get() function will set your variables to "" if it can't find them in that dict
If you use classes instead, you can avoid using global variables, and could even update the config to return a database object functionally, by writing a get_db_object
function, so that you can just use config.get_db_object() to create your db object
Upvotes: 0
Reputation: 42736
You should declare your variables as global
in the init
function, otherwise you are just setting some local variables of the function shadowing the global ones:
def init():
"""docstring"""
global Default_MSSQLPassword
global Default_MSSQLUser
global Default_OdbccDriver
global MDGSQLP01
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"]
As you say in the comment, when importing into the session that variables are bind to Main.Config.your_var_name
for solving this should be enough to declare them as global also in the module:
#Config.py
import os
import json
global Default_MSSQLPassword
global Default_MSSQLUser
global Default_OdbccDriver
global MDGSQLP01
Default_MSSQLPassword = ""
Default_MSSQLUser = ""
Default_OdbccDriver = ""
MDGSQLP01 = ""
def init():
"""docstring"""
global Default_MSSQLPassword
global Default_MSSQLUser
global Default_OdbccDriver
...
Upvotes: 4