Because Python lacks a main() function, when the command to run a Python program is given to the interpreter, the code at level 0 indentation is executed. However, it will first define a few special variables. One such special variable is __name__. If the source file is run as the main program, the interpreter assigns the value “main” to the __name__ variable. If this file is imported from another module, __name__ will be set to the name of the module.
What is __name__ Variable?
- In Python, the
__name__variable is a special built-in variable that contains the name of the current module.
- It is automatically set when a module is imported or run as the main program.
- The value
__name__is set to the string
"__main__"when a Python script is run directly (for example, by running
python myscript.pyfrom the command line). This allows you to use the
if __name__ == "__main__"idiom to determine whether a module is being run as the main program or being imported as a module.
For example, consider the following script
def main(): print("This is the main function.") if __name__ == "__main__": main()
If you run this script directly, by running
python myscript.py, it will output “This is the main function.” However, If you import this script as a module in another script, the if-statement will not be executed and the main() function will not be called.
This can be useful for running certain code only when the module is run directly, but not when it is imported as a module. It also can be used to organize the code in the way that while importing the module it will only import the functionalities, methods, and classes it will not execute the code which is in the if name == “main” block.
Using if __name__ == __main__:
if __name__ == "__main__" is a common idiom in Python to determine whether a module is being run as the main program or being imported as a module. It allows you to separate the code that should only be executed when the module is run directly from the code that should always be available when the module is imported.
Here is an example that demonstrates the usage of
if __name__ == "__main__" idiom :
def my_func(): print("This function is available when the module is imported.") if __name__ == "__main__": print("This code will only be run when the module is executed directly.") my_func()
This code will only be run when the module is executed directly. This function is available when the module is imported.
However, if you import this module into another script, the code inside the
if block will not be executed. Only
my_func() will be available for use in the other script.
This approach allows you to write and test the
main() or any other function in your script separately from the functions which you want to import.
__name__: Printing it’s Value
You can print the value of the
__name__ variable to see what it is set to. The value of
__name__ will be different depending on whether the module is being run directly or imported as a module.
# mymodule.py def my_func(): print("This function is always available when the module is imported.") print("The value of __name__ is:", __name__)
The value of __name__ is: __main__
Note: also read about Barrier Object in Python
Please follow me to read my latest post on programming and technology if you like my post.
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.