Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, the special variable __name__
is used to determine if a Python file is being executed as the main program or if it is being imported as a module into another script. The expression __name__ == '__main__'
is a common idiom to check if the current script is being run as the main program. In this tutorial, we will cover the basics of the __name__
variable and how to use the __name__ == '__main__'
expression in your Python scripts.
Understanding the __name__
variable
When you execute a Python script, the interpreter assigns a special variable called __name__
with a value depending on how the script is executed:
__name__
variable to '__main__'
.__name__
variable to the name of the module (without the .py
extension).Using the __name__ == '__main__'
expression
The __name__ == '__main__'
expression is a common way to ensure that certain parts of your Python script are only executed when the script is run as the main program, and not when it is imported as a module into another script.
This is useful when you want to separate the module's functionality from the code that should only be executed when the script is run directly.
Example: Using __name__ == '__main__'
in a Python script
Let's create a simple script called my_module.py
that contains a function and some code that should only be executed when the script is run as the main program:
# my_module.py def greet(name): return f"Hello, {name}!" if __name__ == '__main__': user_name = input("Enter your name: ") print(greet(user_name))
In this example, the greet()
function can be imported and used in other scripts. However, the code inside the if __name__ == '__main__':
block will only be executed when my_module.py
is run as the main program.
my_module.py
directly:$ python my_module.py Enter your name: John Hello, John!
my_module.py
as a module:# main.py from my_module import greet print(greet("Jane")) # Output: Hello, Jane!
When you run main.py
, the input()
and print()
statements inside the if __name__ == '__main__':
block in my_module.py
will not be executed.
In conclusion, the __name__ == '__main__'
expression is a useful idiom in Python to control which parts of your script should be executed when the script is run as the main program, and which parts should be executed when it is imported as a module. By using this technique, you can create more reusable and maintainable code.
How to use if __name__ == '__main__'
for script execution:
if __name__ == '__main__':
block ensures that a specific block of code is executed only when the Python script is run directly, not when it is imported as a module.def main(): print("This is the main function") if __name__ == '__main__': main()
Avoiding code execution on module import with __name__
:
if __name__ == '__main__':
, you can prevent certain code from being executed when the script is imported as a module.def helper_function(): print("This won't run on import") if __name__ == '__main__': print("This will run only if the script is executed directly")