I have follow this method to create a settings file with globals.
settings.py I have :
def init() global test test = True
in main.py :
import settings settings.init() print(globals())
I cannot see “test” in globals ! Any idea please ?
Advertisement
Answer
In Python, global variables are only global in the module where they were defined.
If you want to access the global variables of an imported module, you can use:
settings_globals = vars(settings)
Or if you only want the public ones (not starting with a leading underscore):
settings_publics = {k: v for k,v in vars(settings).items if not k.startswith('_')}