Abort execution of a module in Python

Viewed 8466

I'd like to stop evaluation of a module that is being imported, without stopping the whole program.

Here's an example of what I want to achieve:

main.py

print('main1')
import testmodule
print('main2')

testmodule.py

print(' module1')
some_condition=True
if some_condition:
  exit_from_module()  # How to do this?
print(' module2')     # This line is not executed.

Expected output:

main1
 module1
main2
3 Answers
Related