I have a function within a module that lists defined variables
Simplified Example:
for var_name in globals():
if not var_name.startswith('__'):
var_value = eval(var_name)
var_type = type(var_value)
print(var_name, "is", var_type, "and is equal to ", var_value)
This works when I run it from within the origin module, but not when the module is imported to another script. It only ever reads the variables defined in the origin module.
I also tried:
import __main__
for var_name in __main__.__dict__:
But that didn't work either. How can I get this function to work when imported into another script?
Thanks!
globals, despite its name, is only global to a single file. Python does not have the concept of globals spanning multiple files or modules, and that's a Good Thing.__main__.__dict__should do what you want.__main__.__dict__, I just wrote it wrong in my question!