简体   繁体   中英

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 ?

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('_')}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM