Before executing code, Python interpreter reads source file and define few special variables/global variables.
If the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module’s name. Module’s name is available as value to __name__ global variable.
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
When we execute file as command to the python interpreter,
- All of the code that is at indentation level 0 [Block 1] gets executed. Functions and classes that are defined are, well, defined, but none of their code runs.
- Here, as we executed script.py directly __name__ variable will be __main__. So, code in this if block[Block 2] will only run if that module is the entry point to your program.
- Thus, you can test whether your script is being run directly or being imported by something else by testing __name__ variable.
- If script is getting imported by some other module at that time __name__ will be module name.
Why Do we need it?
For example we are developing script which is designed to be used as module:
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:
- Every Python module has it’s __name__ defined and if this is ‘__main__’, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
- If you import this script as a module in another script, the __name__ is set to the name of the script/module.
- Python files can act as either reusable modules, or as standalone programs.
- if __name__ == “main”: is used to execute some code only if the file was run directly, and not imported.
This article is contributed by Nirmi Shah. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Print the initials of a name with last name in full
- Python IMDbPY – Getting Person name from searched name
- GUI application to search a country name from a given state or city name using Python
- Python Main Function
- PyQt5 – How to change background color of Main window ?
- PyQt5 - Get opacity level of the main window | windowOpacity() method
- Python | range() does not return an iterator
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- How does the functools cmp_to_key function works in Python?
- Why does Python automatically exit a script when it’s done?
- What does the Double Star operator mean in Python?
- Program to print its own name as output
- PyQt5 QSpinBox – Setting object name
- Convert a column to row name/index in Pandas
- Python | Pandas TimedeltaIndex.name
- Setting the name of the axes in Pandas DataFrame
- Python | Gender Identification by name using NLTK
- PyQt5 QSpinBox – Adding action when object name changed
- Python | Pandas tseries.offsets.DateOffset.name
- Python | Pandas tseries.offsets.BusinessDay.name
Improved By : Sapanz