Skip to content
Advertisement

Get globals() dict from other module

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('_')}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement