I would like to do the following (python3):
In module settings.py
:
message = 'Hello'
In module __main__.py
:
from . import settings
def dict_from_module(module):
...
return d
print(dict_from_module(settings))
Running this should produce:
{'message': 'hello'}
Is there a canonical way of converting a module to a dictionary?
EDIT
Using vars(settings)
gives lots of internal information:
{
'__builtins__': {
...
},
'__cached__': 'xxx/__pycache__/settings.cpython-34.pyc',
'__doc__': None,
'__file__': 'xxx/settings.py',
'__loader__': <_frozen_importlib.SourceFileLoader object at 0x7f87fc192518>,
'__name__': 'xxx.settings',
'__package__': 'xxx',
'__spec__': ModuleSpec(...),
'message': 'bye'
}
Which I do not want / need. I can filter that out (by removing keys starting with __
), but I would like to avoid hacking around if there is an accepted way of doing this.
__doc__
. – Timbre