__name__ (A Special variable) in Python Last Updated : 28 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Since there is no main() function in Python, when the command to run a python program is given to the interpreter, the code that is at level 0 indentation is to be executed. However, before doing that, it will define a few special variables. __name__ is one such special variable. If the source file is executed as the main program, the interpreter sets the __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. __name__ is a built-in variable which evaluates to the name of the current module. Thus it can be used to check whether the current script is being run on its own or being imported somewhere else by combining it with if statement, as shown below. Consider two separate files File1 and File2. Python3 # File1.py print ("File1 __name__ = %s" %__name__) if __name__ == "__main__": print ("File1 is being run directly") else: print ("File1 is being imported") Python3 # File2.py import File1 print ("File2 __name__ = %s" %__name__) if __name__ == "__main__": print ("File2 is being run directly") else: print ("File2 is being imported") Now the interpreter is given the command to run File1.py. python File1.py Output : File1 __name__ = __main__ File1 is being run directly And then File2.py is run. python File2.py Output : File1 __name__ = File1 File1 is being imported File2 __name__ = __main__ File2 is being run directly As seen above, when File1.py is run directly, the interpreter sets the __name__ variable as __main__ and when it is run through File2.py by importing, the __name__ variable is set as the name of the python script, i.e. File1. Thus, it can be said that if __name__ == "__main__" is the part of the program that runs when the script is run from the command line using a command like python File1.py. Comment More infoAdvertise with us Next Article __name__ (A Special variable) in Python H haragr Follow Improve Article Tags : Technical Scripter Python Practice Tags : python Similar Reads How to Learn Python from Scratch in 2025 Python is a general-purpose high-level programming language and is widely used among the developersâ community. Python was mainly developed with an emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code.If you are new to programming and want to lea 15+ min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python 3 basics Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.11.0, we can simply call it Python3. Python 3.0 was released in 2008. and is interpreted language i.e it's not compiled and the interpreter will check the code line by line. This article can be used to learn the 10 min read Important differences between Python 2.x and Python 3.x with examples In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling 5 min read Download and Install Python 3 Latest Version If you have started learning of Python programming, then for practice, Python installation is mandatory, and if you are looking for a guide that teaches you the whole process from downloading Python to installing it, then you are on the right path.Here in this download and install Python guide, we h 6 min read Statement, Indentation and Comment in Python Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Pytho 7 min read Python | Set 2 (Variables, Expressions, Conditions and Functions) Introduction to Python has been dealt with in this article. Now, let us begin with learning python. Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on th 3 min read Global and Local Variables in Python In Python, global variables are declared outside any function and can be accessed anywhere in the program, including inside functions. On the other hand, local variables are created within a function and are only accessible during that functionâs execution. This means local variables exist only insi 7 min read Type Conversion in Python Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. This article is aimed at providing information about certain conversion functions. There are two types of Type Conversion in Python: Python Implicit Type C 5 min read Private Variables in Python Prerequisite: Underscore in PythonIn Python, there is no existence of âPrivateâ instance variables that cannot be accessed except inside an object. However, a convention is being followed by most Python code and coders i.e., a name prefixed with an underscore, For e.g. _geek should be treated as a n 3 min read Like