Python: Run Module as Script

(Last Updated On: )

We can run a Python module as a script directly. In order to do that it checks that “__name__” is set to “__main__”. If it is then it executes the code in the if block. See below for example.

  1. #imports here
  2.  
  3. def run():
  4. #run code here
  5. return
  6.  
  7. if __name__ == "__main__":
  8. run()