#! /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. Whats's wrong
[tim@tim-arch ~]$ gvim script
[tim@tim-arch ~]$ sudo chmod 775 script
[tim@tim-arch ~]$ ./script
[tim@tim-arch ~]$
If you find the other answers confusing or intimidating, here's a parable which should hopefully help. Look at the following Python program:
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.The following program is in some sense very similar to the first one.
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 declarationb
is distinct from the global variable with the same name.)You need to call that functions, update script to
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:or
In your case your code could be turned to something like:
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:
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):The above is working as following:
import
a python module, it gets astring
(usually, the name under which it was imported) assigned as its__name__
attribute.__name__
attribute is set to__main__
import
ing it at will, or just execute it directly to have the series of expressions under theif __name__ == '__main__':
be evaluated directly.Should you feel the need to dig through more information, my sources were the following:
__name__
)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__
.With just these lines of code:
you're defining a function and not actually invoking it. To invoke the function
main()
, you need to call it like this:You still have to call the function.