Why doesn't main?

It so happened that I started with django, not penetrating into the basics of Python. Now just wanted something in the console to test and realized that I do not know but there is generally a main or here, as in php you have to write directly without a function code that he volunteered? Tried to create a function named main, but for some reason it is not called. Although the code fulfills without errors. And found what that is a reserved word __main__. Tried with the same name, the create function is also not called automatically... So how then?
July 2nd 19 at 18:21
3 answers
July 2nd 19 at 18:23


def foo(): print('bar')

def buzz(): print('buzz')

if __name__ == "__main__": 

def foo(): print('bar')

from f1 import buzz 
I do know how to Google. There there is no one answer to this question. It's all about what the If __name__ == '__main__' write. And this is not it. - kurt_Jacobson commented on July 2nd 19 at 18:26
: exactly - marilyne_Roh commented on July 2nd 19 at 18:29
: pep8.ru/doc/dive-into-python-3 better read - graham commented on July 2nd 19 at 18:32
: Yes, I don't need to learn the basics. I'm just about this specific detail in the course. I know a lot of other languages and super easy to learn Python on the go. Just there are nuances like that which in all languages in different ways. - kurt_Jacobson commented on July 2nd 19 at 18:35
Yeah I get it. So main like as not need? Here it is possible without functions, just write and everything works. Then the meaning of the main-e? - kurt_Jacobson commented on July 2nd 19 at 18:38
: to code placed in main, is executed only when you run this file, but not when it is imported. - Annabel_Hodkiewicz3 commented on July 2nd 19 at 18:41
: What is __name__ and __main__? - kurt_Jacobson commented on July 2nd 19 at 18:44

read the answers FROM

Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module''s name. - marilyne_Roh commented on July 2nd 19 at 18:47
July 2nd 19 at 18:25
The code works immediately. If you have written the main function should be to call separately
def main():
July 2nd 19 at 18:27
The name "main" in Python is not reserved, just the main() function is often used, by analogy with sysname programs (for this function, see what the program does and where it all starts).

And gate
if __name__ == '__main__':
need to exclude some code from the import.
Each file .py is a Python module, you can import it as a module and use the contents of it (functions, classes, and other objects). But by itself, the file does something, if its run by itself. When you import the whole module first runs, but if you want only a function module and you do not need to perform that for which the whole unit is done, it will still run the whole. Therefore, in the module placed gates, which pass only when the module runs as a separate file. When import it, the gate is not passed.

In vain you started learning Django, because there is nobody to explain these basics, and this is really the very basics of Python.

Find more questions by tags Python