Python main

You want to write a new Python script, but a blank file stares back at you ... What is the best way to get started?

Here's a reasonable snippet to get going.

def main():
  print("hello, world!")

if __name__ == '__main__':
  main()

This allows you to use invoke the script from the command line, as well as to import whatever you rwite from some future script. In that case module name will not be __main__ and thus not invoke your function, but you'll be able to get to other declarations.

What should go in main? Great question for the next post.

Happy Python coding!

Tags:  codingpython

Home