I've 3 files. One is for defining the variables and other two contain the required modules.
variable.py
my_var = ""
test.py
import variable
def trial():
variable.my_var = "Hello"
main.py (not working)
from variable import *
from test import trial
if __name__ == "__main__":
trial()
print my_var
And when I run main.py, it gives nothing. However, if I change main.py like this,
main.py (working)
import variable
from test import trial
if __name__ == "__main__":
trial()
print variable.my_var
And it gives me the expected output i.e. Hello.
That variable.py, in my case, contains more variables and I don't want to use variable.<variable name> while accessing them. While modifying them, using variable.<variable name> is not a problem as I'm gonna modify only once depending on the user's input and then access them across multiple files multiple times rest of the time.
Now my question is, is there a way to extract all the variables in main.py after they are modified by test.py and use them without the prefix variable.?
from variable import *you are adding the names from the modulevariableto the local namespace, hence they references point to the values of the variables when the import happens. If now you change the original module, that change will not be reflected in your imported symbols.variable.?variable.will be better readable, I know. But using this 100 times is kind of not necessary when one knows there won't be any local variables and all variables used are coming from one single file.