You want to check in your settings.py
file to see if that file was run directly by the user, or if it was included by some other module. The way to do that is with the __name__
variable. That variable is always defined by Python. If you are in the file that was run directly, it's value will be __main__
. So, in settings.py
, do something like the following:
<code you want to always have take effect, usually functions, class definitions, global variables, etc.>
if __name__ == '__main__':
<code you only want to run if this file was run directly>
It is common to use this mechanism to provide some test code for a module. Here's simple concrete example of that:
def some_function(a, b, c):
print(a, b, c)
def test():
some_function(123, 234, 456)
if __name__ == '__main__':
test()
So this just defines some_function
and test
, but doesn't run them if it is included by some other Python file. If it is run directly, then test()
is run to test the some_function()
function.