I have a python module, where i store my settings settings.py. This files contains lines like
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_URL = 'https://my-base-url.com/test'
...
SCORM_TEST_URL = BASE_URL + '/scorm2004testwrap.htm'
I want to allow users to override these settings e.g. by using command line options or environment variables. It works well for direct variable assignments when I do
import settings
settings.BASE_URL = 'test'
However
settings.SCORM_TEST_URL
#will print https://my-base-url.com/test/scorm2004testwrap.htm
Is there a way to tell python to update these dependant variables with the overridden values? Or how can I design my program, that it allows overriding of settings and as well setting variables in files that are python modules, because as you see it might need other python modules imports like os in the above example?
1 Answer 1
Seems like this is not something doable
When you set setting.BASE_URL = something, its being reassign to something now and whatever previous value is gone so it works.
But SCORM_TEST_URL is created based on old BASE_URL value, its has no relation to BASE_URL once its created.Thus, changing BASE_URL won't reflect on SCORM_TEST_URL unless you reevaluate it based on new BASE_URL value.
Some workaround i can think of as below, declare those variable that need to modify outside module as usual.
Put another set of variable that needs to be updated inside an update function and make them as global
Inside settings.py
BASE_URL = 'https://my-base-url.com/test'
# ... other var
def update_var():
global SCORM_TEST_URL
SCORM_TEST_URL = BASE_URL + '/scorm2004testwrap.htm'
# added other var accordingly
From other module, import settings as usual
import settings
settings.BASE_URL = 'test'
# update variables
settings.update_var()
# settings.SCORM_TEST_URL should be updated now