if __name__ == ‘__main__’
Modules are Python files, and the.py extension is used to distinguish them. Function definitions, class declarations, and variable declarations are all within the purview of a module’s scope. It follows that if the module being executed by the interpreter is the main program, the “__name__” variable will be set to “__main__”. The __name__ variable will be replaced with the name of the importing module if the code does so. Consider this illustration. Make a new Python file and call it file one.py; next, copy and paste this code into it.
# Python file one module print("File one __name__ is set to: {}" .format(__name__))
By running this file you will see exactly what we were talking about. The variable __name__ for this module is set to __main__:
Python’s interpreter examines the source code and sets up a small set of special variables and global variables before running the program. For modules (code files) being executed by the Python interpreter as the main program, the interpreter assigns the value “__main__” to the special __name__ variable. If this file was imported from another module, the name of that module will be substituted for __name__. The __name__ global variable can be set to the name of a module. One way to organize Python code is in a “module,” which is a separate file. Simply append the.py extension to the module’s name to get the file name. Assuming that we feed the file to the python interpreter as an instruction,
python script.py
# Python program to execute # main directly print ("Always executed") if __name__ == "__main__": print ("Executed when invoked directly") else: print ("Executed when imported")
The entirety of the code that is located at indentation level 0 within Block 1 is carried out. The classes and functions that have been defined are, in a sense, defined; nonetheless, none of their code is executed.
Right here, while we carried out the script.
“__name__” will be set to “__main__” for the py directly variable. Therefore, the code contained in this if block [Block 2] will only be executed if the aforementioned module serves as the starting point for your program. Therefore, by examining the __name__ variable, you may determine if your script is being executed directly or whether it is being imported by something else. If another module imports the script, the name of the module will be contained within the __name__ variable at that point.
Why do we need it?
For example we are developing script which is designed to be used as module:
# Python program to execute function directly def my_function(): print ("I am inside function") # We can test function by calling it. my_function()
Now if we want to use that module by importing we have to comment out our call. Rather than that approach best approach is to use following code:
# Python program to use main for function call. if __name__ == "__main__": my_function() import myscript myscript.my_function()
Advantages
- The entirety of the code that is located at indentation level 0 within Block 1 is carried out. The classes and functions that have been defined are, in a sense, defined; nonetheless, none of their code is executed.
- Right here, while we carried out the script. __name__ will be set to __main__ for the py directly variable. Therefore, the code contained in this if block [Block 2] will only be executed if the aforementioned module serves as the starting point for your program.
- Therefore, by examining the __name__ variable, you may determine if your script is being executed directly or whether it is being imported by something else.
- If another module imports the script, the name of the module will be contained within the __name__ variabsle at that point.
The __name__ variable is useful whether you’re looking to create a standalone executable or a module that can be imported by others. If the module’s __name__ is “__main__,” we can control whether or not a certain section of code executes after import. The __name__ variable is initialized to __main__ if the module is being run, or to the module’s name if it was imported, when the Python interpreter reads a file. All of the file’s top-level code is run when the file is read, but subroutines and classes are ignored.