SaralGyaan, Saral Gyaan, Saral Gyan is website which provides free tutorials of Python for absolute beginners in English and Hindi, Learn Python in Hindi, Learn Python for absolute beginners, Learn Python, bootstrap, django, git

Hari S
Author: Hari S


If __name__ == '__main__' in Python

  • Aug. 23, 2019, 1:32 p.m.
  • 3 minutes read
  • 75 Views
If __name__ == '__main__' in Python

What does if __name__ == ‘__main__‘in python means?

In python, you must have come accross the following code :-

if __name__ == '__main__':
    main()

In this tutorial, we will learn in detail what does the above statement means. As per official documentation:

‘__main__‘ is the name of the scope in which top-level code executes. A module’s __name__ is set equal to ‘__main__‘ when read from standard input, a script, or from an interactive prompt.

It means that if any module/python file is running as the main program, it sets the special__name__ variable to have a value ‘__main__‘

Create a python file ‘module_one.py’ and type the following code:-

# module_one.py

print f'The name of module one is {__name__}' 

If you will run it. You will get the following output:-

The name of module one is __main__

However, if this module is imported in some other file or module, variable __name-- will be set as the name of the first module.

To make it clear, create another python file (module) called ‘module_two.py’ and import first module in it:-

# module_two.py

import module_one

Since, we have imported module_one in module_two.py, when we will run the second module, it will execute the first module and we will get the following output on running module_two.py:-

The name of module one is module_one

Since, now we are directly running module_two.py, Python will set it’s __name__ as ‘__main__‘

# module_two.py

import module_one

print f'The name of module two is {__name__}' 

Output of this module will be:-

The name of module one is module_one
The name of module two is __main__

Hence if __name__ == ‘__main__‘ checks whether the file is being run directly or it has been imported into some other file. The following example will make it more clear:-

# module_one.py

print "This will always be printed"

if __name__ == "__main__": 
    print "Module one has been run directly"
else: 
    print "Module one has been imported"
# Output
This will always be printed
Module one has been run directly

Now if we just import module_one in module_two.py and run it, we will get the following output.

# module_two.py
import module_one
# Output
This will always be printed
Module one has been imported

We use this if we want to make a certain function run, when a module is run directly and some other function to run if it is imported.

If you have any query, please feel free to ask in comment section.



Related Posts

Python Tutorial for Beginners
By Hari S | 7 months, 4 weeks ago

Python Tutorial for Beginners

Best Python Tutorial, Free Python Tutorial, Python Tutorial Pdf. Does these terms sound familiar, You must have searched about python tutorials in the google search bar. Your search for the Best Python Tutorial is over.

We are excited to bring Python tutorial for a complete beginner ...

Read More
Chapter 7- Numbers, Conversions and Operators
By Hari S | 6 months, 2 weeks ago

In the previous chapter, we learned about various data types. From this chapter onward, we will learn in detail about each data type.

Numbers

This data type stores the numeric values and is immutable. For the beginners, it is sufficient to understand that immutable objects are those which can not ...

Read More
Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5
By Hari S | 3 weeks, 4 days ago

Matplotlib Tutorial in Python

Filling Area on Line Plots | Chapter 5

Matplotlib Line plots are a good way to represent your data. But sometimes you need more than that to make your data self-explaining. In such a case, filling the area between or under a Maplotlib Line Plot ...

Read More
Search