Top-level script environment in Python (__main__)


A module object is characterized by various attributes. Attribute names are prefixed and post-fixed by double underscore __. The most important attribute of module is __name__. When Python is running as a top level executable code, i.e. when read from standard input, a script, or from an interactive prompt the __name__ attribute is set to '__main__'.

>>> __name__
'__main__'

From within a script also, we find a value of __name__ attribute is set to '__main__'. Execute the following script.

'module docstring'
print ('name of module:',__name__)

Output

name of module: __main__

However, for an imported module this attribute is set to name of the Python script. For hello.py module

>>> import hello
>>> hello.__name__
hello

As seen earlier, the value of __name__ is set to __main__ for top-level module. However, for the imported module it is set to name of a file. Run following script (moduletest.py)

import hello
print ('name of top level module:', __name__)
print ('name of imported module:', hello.__name__)

Output

name of top level module: __main__
name of imported module: hello

A Python script containing function may also have a certain executable code. Hence if we import it its code will be run automatically. We have this script messages.py with two functions. In executable part user input is provided as an argument to thanks() function.

def welcome(name):
print ("Hi {}. Welcome to TutorialsPoint".format(name))
return
def thanks(name):
print ("Thank you {}. See you again".format(name))
name = input('enter name:')
thanks(name)

Obviously when we run messages.py output shows a thanks message as below.

enter name:Ajit
Thank you Ajit. See you again

We have a moduletest.py script as below.

import messages
print ('name of top level module:', __name__)
print ('name of imported module:', messages.__name__)

Now we if we run the moduletest.py script, We'll find that the input statement and call to welcome() will be executed.

c:\python37>python moduletest.py

Output

enter name:Kishan
Thank you Kishan. See you again
enter name:milind
Hi milind. Welcome to TutorialsPoint

This is an output of both the scripts. But want to import function from messages module but not the executable code in it.

This is where the fact that value of __name__attribute of top-level script is __main__ is useful. Change messages.py script such that it executes input and function call statement only if __name__ is equal to __main__.

"docstring of messages module"
def welcome(name):
print ("Hi {}. Welcome to TutorialsPoint".format(name))
return
def thanks(name):
print ("Thank you {}. See you again".format(name))
if __name__=='__main__':
name = input('enter name')
thanks(name)

Use above technique whenever you want a module that can be executed as well as imported. The moduletest.py doesn't require any changes. An executable part in messages module won't run now.

enter name: milind
Hi milind. Welcome to TutorialsPoint

Note that this doesn't prevent you from running messages.py script independently.

Updated on: 27-Jun-2020

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements