- 27.9k
- 21
- 122
- 155
What does
if __name__ == "__main__":do?What does
if __name__ == "__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
What does
if __name__ == "__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
What does
if __name__ == "__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
What does
if __name__ == "__main__""__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
What does
if __name__ == "__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
What does
if __name__ == "__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
A Short Answer
What does
if __name__ == "__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
A Short Answer
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()
What does
if __name__ == "__main__":do?
__name__ is a global variable (in Python, global actually means on the module level) that exists in all namespaces. It is typically the module's name (as a str type).
As the only special case, however, in whatever Python process you run, as in mycode.py:
python mycode.py
the otherwise anonymous global namespace is assigned the value of '__main__' to its __name__.
Thus, including the final lines
if __name__ == '__main__':
main()
- at the end of your mycode.py script,
- when it is the primary, entry-point module that is run by a Python process,
will cause your script's uniquely defined main function to run.
Another benefit of using this construct: you can also import your code as a module in another script and then run the main function if and when your program decides:
import mycode
# ... any amount of other code
mycode.main()