user1703914
user1703914

Reputation:

Why doesn't the main() function run? (What is a Python script's entry point?)

I have this code:

import sys

def random(size=16):
    return open(r"C:\Users\ravishankarv\Documents\Python\key.txt").read(size)

def main():
    key = random(13)
    print(key)

When I try running the script, there are no errors, but nothing appears to happen. I expected it to print some content from the key file, but nothing is printed.

What is wrong? How do I make the code run?

Upvotes: 18

Views: 55783

Answers (5)

user764357
user764357

Reputation:

You've not called your main function at all, so the Python interpreter won't call it for you.

Add this as the last line to just have it called at all times:

main()

Or, if you use the commonly seen:

if __name__ == "__main__":
    main()

It will make sure your main method is called only if that module is executed as the starting code by the Python interpreter. More about that here: What does if __name__ == "__main__": do?

If you want to know how to write a 'main' function, Guido van Rossum (the creator of Python) wrote about it in 2003.

Upvotes: 37

Karl Knechtel
Karl Knechtel

Reputation: 61625

Something does happen, it just isn't noticeable

Python runs scripts from top to bottom. def is a statement, and it executes when it is encountered, just like any other statement. However, the effect of this is to create the function (and assign it a name), not to call it. Similarly, import is a statement that loads the other module (and makes its code run top to bottom, with its own global-variable context), and assigns it a name.

When the example code runs, therefore, three things happen:

  • The code for the sys standard library module runs, and then the name sys in our own module's global variables is bound to that module

  • A function is created from the code for random, and then the name random is bound to that function

  • A function is created from the code for main, and then the name main is bound to that function

There is nothing to call the functions, so they aren't called. Since they aren't called, the code inside them isn't run - it's only used to create the functions. Since that code doesn't run, the file isn't read and nothing is printed.

There are no "special" function names

Unlike in some other languages, Python does not care that a function is named main, or anything else. It will not be run automatically.

As the Zen of Python says, "Explicit is better than implicit". If we want a function to be called, we have to call it. The only things that run automatically are the things at top level, because those are the instructions we explicitly gave.

The script starts at the top

In many real-world scripts, you may see a line that says if __name__ == '__main__':. This is not "where the script starts". The script runs top to bottom.

Please read What does if __name__ == "__main__": do? to understand the purpose of such an if statement (short version: it makes sure that part of your top-level code is skipped if someone else imports this file as a module). It is not mandatory, and it does not have any kind of special "signalling" purpose to say where the code starts running. It is just a perfectly normal if statement, that is checking a slightly unusual condition. Nothing requires you to use it in a script (aside from wanting to check what it checks), and nothing prevents you from using it more than once. Nothing prevents you from checking whether __name__ is equal to other values, either (it's just... almost certainly useless).

Upvotes: 6

alfonso.kim
alfonso.kim

Reputation: 2900

There's no such main method in python, what you have to do is:

if __name__ == '__main__':
    main()

Upvotes: 3

BrenBarn
BrenBarn

Reputation: 251488

You're not calling the function. Put main() at the bottom of your code.

Upvotes: 0

TerryA
TerryA

Reputation: 60014

Python isn't like other languages where it automatically calls the main() function. All you have done is defined your function.

You have to manually call your main function:

main()

Also, you may commonly see this in some code:

if __name__ == '__main__':
    main()

Upvotes: 13

Related Questions