Trying to dynamically add settings attributes to globals()
according to some dynamic environment variables.
import os
from importlib import import_module
# To support environment variables as the means of specifying
# the target environment as an alternative to using the --settings flag
# where MY_ENV is 'local', 'development', 'staging', 'production'
if os.environ.get('MY_ENV'):
env = os.environ.get('MY_ENV')
import_module('my.settings.%s' % env, '*')
Its understood that import_module
needs to be assigned to a variable:
foo = import_module('path.to.package', 'bar')
and that:
globals()[foo] = foo
can add this to the global
scope.
However, what is to be done with the wildcard *
?
globals()[*] = import_module('path.to.package', '*')
Obviously, the above causes a syntax error.