Let’s say I’ve a file a.py and a has a variable var.
In file b.py, I imported, a.py and set a.var = "hello".
Then I imported a.py in c.py and tried to access a.var but I get None.
How to reflect the change in data? It is obvious that two different instances are getting called, but how do I change it?
Edit:
What I am trying to do here is create a config file which constitutes of this:
CONFIG = {
'client_id': 'XX',
'client_secret': 'XX',
'redirect_uri': 'XX'
}
auth_token = ""
auth_url = ""
access_point = ""
Now, let’s say I use config.py in the script a.py and set config.access_point = “web”
So, if I import config.py in another file, I want the change to reflect and not return “”.
Edit:
Text file seems like an easy way out. I can also use ConfigParser module. But isn’t it a bit too much if reading form a file needs to be done in every server request?
As a preliminary, a second
importstatement, even from another module, does not re-execute code in the source file for the imported module if it has been loaded previously. Instead, importing an already existing module just gives you access to the namespace of that module.Thus, if you dynamically change variables in your module a, even from code in another module, other modules should in fact see the changed variables.
Consider this test example:
testa.py:
testb.py:
testc.py:
Now in the python interpreter (or the main script, if you prefer):
So you see that the change in
varmade when importing b is actually seen in c.I would suspect that your problem lies in whether and in what order your code is actually executed.