#! /usr/bin/python def main(): print("boo")
This code does nothing when I try to run it in Python 3.3. No error or anything.
[tim@tim-arch ~]$ gvim script [tim@tim-arch ~]$ sudo chmod 775 script [tim@tim-arch ~]$ ./script [tim@tim-arch ~]$
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
#! /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:
You need to call that functions, update script to
#! /usr/bin/python def main(): print("boo") #call it main()
In python, if you want to write a script to perform a series of small tasks sequentially, then there is absolutely no need to write a function to contain them. Just put each on a line on its own; or use an expression delimiter like
; (not really recommended, but you can do is you so desire), likewise:
task1 task2 task3 task4
task1; task2; task3; (again **not** really recommended, and certainly not pythonic)
In your case your code could be turned to something like:
print('boo') print('boo2') print('boo3')
and it would still act as you expect it to, without the
main() method, as they get evaluated sequentially.
Please note that the reason you might want to create a function for these series of tasks is:
- to present a nice interface (to clients of the code),
- or to encapsulate repeated logic
- There might be more uses, but that’s the first I can come up with, and serve to prove my point.
Now, if you feel compelled to write code that resembles the
main() method in other programming languages, then please use the following python idiom (as stated by other users so far):
if __name__ == '__main__': doSomething()
The above is working as following:
- When you
importa python module, it gets a
string(usually, the name under which it was imported) assigned as its
- When you execute a script directly (by invoking the python vm and passing it the script’s name as an argument), the
__name__attribute is set to
- So when you use the above idiom, you can both use the script as a pluggable module by
importing it at will, or just execute it directly to have the series of expressions under the
if __name__ == '__main__':be evaluated directly.
Should you feel the need to dig through more information, my sources were the following:
If you find the other answers confusing or intimidating, here’s a parable which should hopefully help. Look at the following Python program:
a = 34
When it runs, it does something: before exiting the script, Python learns that there is a variable
a and that its value is the integer 34. It doesn’t do anything with this information, but it’s a complete program which does something. In order for it to produce some actual value, it needs to interact with its environment, though. What does it do with this value? It could create 34 directories, or ping the 34th server in your data center, or check the strength of the passwords of the newest 34 users in your database, or whatever; or just print something.
a = 34 print(a)
The following program is in some sense very similar to the first one.
def b(): a = 34 print(a)
When you run this program, it does something: Python now knows that there is a function named
b, and that it doesn’t take any arguments, and that it contains some syntactically valid Python code which will be run when some other code calls it, but it doesn’t actually do anything with this code yet. In order to observe any value being produced by the code in the function, you have to actually call it:
(As an aside, maybe also note that the local variable
a inside he function declaration
b is distinct from the global variable with the same name.)