main() function doesn't run when running script
You still have to call the function.
def main(): # declaring a function just declares it - the code doesn't run
print("boo")
main() # here we call the function
I assume what you wanted to do is call the print function when the script is executed from command line.
In python you can figure out if the script containing a piece of code is the same as the script which was launched initially by checking the __name__
variable against __main__
.
#! /usr/bin/python
if __name__ == '__main__':
print("boo")
With just these lines of code:
def main():
print("boo")
you're defining a function and not actually invoking it. To invoke the function main()
, you need to call it like this:
main()