When you import a module in python, the module code is "run". Sometimes it is useful to have branching logic in the module such as checking package versions or platform or whatever. Is there a way to exit the entire module execution before hitting the end of the file, something equivalent to early return in a function?
If the module is run as a script you can exit() but that actively raises an exception and kills the whole thing. I just want to say, that's it you are done now, don't run any more code below here.
Basically can I transform this
if not <condition>:
MY_CONSTANT = 3.14
class blah():
...
def foo(x):
...
# rest of module....
into
if <condition>:
return from module
MY_CONSTANT = 3.14
class blah():
...
def foo(x):
...
# rest of module....
mostly so that I don't have to have lots of code that looks strangely one extra indent level in.