If you are having trouble finding the cause and solution of the error NameError: name ‘main’ is not defined in Python, this article may help you. In this article, I will identify the cause of the error and perform a fix by putting name main in quotes or on the Python version you are using. Post details below.
What causes the NameError: name ‘__main__’ is not defined error?
The statements contained in a source code file are all executed by the Python interpreter as it reads the file. Python gives the variable (__name__) the value (“__main__”) when it executes the “file containing the source code” as the main program.
When the main function is called, the “if” statement is read, and a check is made to see if the __name__ variable contains the value “__main__”. Python’s “__if name __” == “__main__” function enables you to launch source code files as standalone applications or reusable modules.
The main function has two applications in the Python interpreter:
- __name__ is the module’s file name when it is used to load. If the condition in the if statement is not met, the __main__ source code will not be run.
- __name__ = __ main__ when used to run something directly. The source code in __main__ will be run if the condition in the if statement is met.
To validate the module name, the source code uses the “if” statement when it is run.
The error NameError: name ‘__main__’ is not defined happens you don’t put name __main__ in quotes.
Example:
def main(): print('This is a Python tutorial for beginnners.') print('Hello') if __name__ == __main__: main()
Output:
Traceback (most recent call last):
File "./prog.py", line 6, in <module>
NameError: name '__main__' is not defined
How to solve this error?
Put name __main__ in quotes.
Example:
- To fix the error NameError: name __main__ is not defined, enclose the special name __main__ in quotes.
def main(): print('This is a Python tutorial for beginnners.') print('Hello') # Enclose the special name __main__ in quotes. if __name__ == '__main__': main()
Output:
Hello
This is a Python tutorial for beginnners.
On Python 2 and Python 3 versions.
In Python 3, you can remove if__name__, and the program will still function properly.
Example:
def main(): print('This is a Python tutorial for beginnners.') print('Hello') # Remove if__name__, and the program will still function properly. main()
Output:
Hello
This is a Python tutorial for beginnners.
If you are still using Python 2.
Example:
def main(): print('This is a Python tutorial for beginnners.') print 'Hello' # Enclose the special name __main__ in quotes. if __name__ == '__main__': main()
Output:
Hello
This is a Python tutorial for beginnners.
Summary
I have shown the causes and given the remedies that fixed the error NameError: name ‘__main__’ is not defined in Python. This is a simple bug in Python. Remember to import it before using it. Please fix it as soon as possible. Good luck!

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java