Unable to stop module execution. You can throw an exception, but then you need your importing module. Maybe just refactoring:
print(' module1') some_condition = True if not some_condition: print(' module2')
Update. It would be even better to change your module to define only functions and classes, and then call one of them for the caller to do the work they have to do.
If you really want to do all this work during import (remember, I think it would be better not to do this), then you can change your module in this way:
def _my_whole_freaking_module(): print(' module1') some_condition = True if some_condition: return print(' module2') _my_whole_freaking_module()
Ned batchelder
source share