Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if __name__ == '__main__' python

Tags:

python

I have gone through so many articles regarding this:

if __name__ == '__main__'

But I didn't get it... I am going to share you code. What is a more brief explanation?

I have created one file, "ab.py":

def a():
    print('A function in ab file');

a()

The second file is "xy.py"

import ab

def b():
    print('b function')

def x():
    print ('s');

x()

if __name__ == "__main__" :
    b()

When I execute this code then this output is coming:

A function in ab file
s
b function

What does this mean? What is this code actually doing? Why do we implement this? Our code is also working without it.

if __name__ == "__main__":
    b()
like image 639
Jaskaran Zap Avatar asked Feb 05 '15 05:02

Jaskaran Zap


People also ask

What does if __name__ == ‘__main__’ mean in Python?

Let’s have a look at what it means and why you should know about it. The condition if __name__ == ‘__main__’ is used in a Python program to execute the code inside the if statement only when the program is executed directly by the Python interpreter. When the code in the file is imported as a module the code inside the if statement is not executed.

What is the __name__ variable used for in Python?

The Python interpreter sets the __name__ variable before executing the Python script. When you run a module directly, the value of the __name__ is __main__. When you import a module inside another Python script, the value of the __name__ is the module name.

What does the word __name__ mean in Python?

The word __name__ in Python represents a special variable. There are many special variables in Python that start and end with double underscores. To make it short they are referred to as dunder (from Double Underscores). So __name__ is pronounced “dunder name”. Let’s see what the value of __main__ is by using the Python shell:

What happens when Python read a file?

When the Python interpreter reads a file, the __name__ variable is set as __main__ if the module being run, or as the module's name if it is imported. Reading the file executes all top level code, but not functions and classes (since they will only get imported).


Video Answer


2 Answers

You should get in the habit of using this almost always.

Anything that comes after if __name__ == '__main__': will be run only when you explicitly run your file.

python myfile.py

However, if you import myfile.py elsewhere:

import myfile

Nothing under if __name__ == '__main__': will be called.

like image 63
Adam Hughes Avatar answered Oct 05 '22 08:10

Adam Hughes


A really simple example to understand this statement is the following:

Assume that we have the following Python script named: using_name.py:


# Filename: using_name.py

if __name__ == '__main__':
    print 'This program is being run by itself'
else:
    print 'I am being imported from another module'

Now, try to do the following two things and see what happens:


1) Run directly the script

python using_name.py

Result

This program is being run by itself

2) Import the script

python

import using_name

Result

I am being imported from another module
like image 32
seralouk Avatar answered Oct 05 '22 07:10

seralouk