shaikhu-blog-v1626978481/shaikhu/image_jyuasz.jpg

Main function in Python and how to use __name__ variable?

Published on 2020-11-06, by Javed Shaikh

Subscribe for new article
*No spam. unsubscribe at anytime

Most of the high level languages such as Java,C/C++ have special main method defined as the execution entry point. This tells interpreter where to start the execution of codes. Its like an entry point of a program. If you don't define a main method in such languages, your code is never going to execute. But in case of Python, its totally different. In Python, interpreter executes codes from top to bottom by default irrespective of main function.

In this post we are going to try different cases with some examples to understand the concept of main function in Python.

Is there a main function in python

Yes there can be a main function in Python but interpreter doesn't care if it is present or not. Interpreter will execute codes right from the start of the program.

Python has a special variable called __name__ that will tell the name of the program or function currently being executed.

Two things to note about __name__

  • Python sets this variable to __main__ when the same file gets executed as the main program.
  • When Python interpreter imports codes or programs from another file, the interpreter sets this variable to the name of that module or file.

Lets try some examples to know it better.

CASE 1: main function in a Python script

Lets create a new file and name it as test1.py and just put this one line and execute.

python
1#test1.py 2print('Hello from test1, value of __name__ is',__name__) 3
OUTPUT: Hello from test1, value of __name__ is __main__

As shown when we run this file, it prints the value __main__ because this is the main program which is being executed directly and not being imported

CASE 2:

If we run below program, output will still be similar

python
1#test1.py 2print('in Test1, __name__:',__name__) 3 4if __name__ == "__main__": 5 print("hello from ",__name__) 6

OUTPUT: in Test2,__name__:__main__ hello from __main__

CASE 3: define a main function or entry segment

Since Python runs codes from start to end, its always a good practice to define a entry point or function.

As shown below, test2 is the main function for this program

python
1#test2.py 2def test2(): 3 print('test2 function,value of __name__:',__name__) 4 5if __name__ == "__main__": 6 test2() 7
OUTPUT:
test2 function,value of __name__: __main__

Here, Interpreter will call function test2() only when its executing main function else it will not call this function even though interpreter will start execution from top. If we run this code as a main program without importing to another file, this will print __main__ as shown above

CASE 4: main function when importing another file

As we mention before if Python interpreter imports codes from another file, the interpreter sets this variable to the name of that module or file.

Lets check it out.

python
1#test3.py 2from test2 import * 3 4def test3(): 5 test2() 6 print('test3 function,value of __name__:',__name__) 7 8if __name__ == "__main__": 9 test3() 10

Guess the output. We are calling test3() if this is a main program and inside test3() we are calling test2() function imported from another file test2.py

OUTPUT:
test2 function,value of __name__: test2
test3 function,value of __name__: __main__

Things to note again

When we ran test2.py in CASE 3 example, __name__ was __main__ but when we ran same file imported into test3.py, the __name__ became the name of the module or file name 🙂

Best Practice

Since we saw how in Python, codes are executed from first line, its always a good practice to define a main function and call all other functions and code blocks from there. See the example below

python
1#test4.py 2from test2 import * 3 4def test3(): 5 test2() 6 print('test3 function,value of __name__:',__name__) 7 8def main(): 9 print('this is main function') 10 test3() 11 12if __name__ == "__main__": 13 main() 14

In above example, our code will start execution from main() function and will call others from inside it.

Conclusion:

Its always important to know entry point to your program. Use of __name__ can be helpful to identify in which program you are in. Since in Python code execution starts from first line of the program, its always a good practice to define a function and put all your changes in the function and call it based on __name__ variable.

Thanks for reading. Let me know your thoughts in the comment section.

My other recent posts

About the Author

I am a Backend System Engineer at a credit card company, specializing in C/C++ and assembler on IBM's TPF OS. I have a passion for web development and enjoy working with Node.js and Python in my free time.

Connect with author

Related articles ...

How to auto change desktop wallpaper every minute using Python

We are going to build a CLI app using Python to change desktop wallpaper every given number of minutes. Every wallpaper downloaded from internet will be unique and our app will change the wallpaper based on the time we set.

2020-10-27

How to get email alert when your website is down using shell and Python?

There are many ways to monitor and set alerts for your web server using third party apps but they don't come with free, be it Pingdom or Amazon's CloudWatch. In this post I am going to show how we can write and setup our own alert tool with just few lines of code

2020-10-29

Files, Folders and Python

In this post we are going to see how we can use Python to find the size of any file and folder. We will check how many files we have inside a folder and how many of them are empty files and how to delete those empty files.

2020-10-26

How to setup alarm for CPU usage using IFTTT ?

Monitoring CPU and memory usage are one of the top todo checklist for a backend engineer. Sometimes you wont even notice when your server is down due to high CPU usage unless you login and manually check the system.

2020-11-04