Python tutorials > Modules and Packages > Modules > What is `__name__`?
What is `__name__`?
In Python, __name__
is a special built-in variable that automatically gets assigned a string value representing the name of the current module. Understanding how __name__
works is crucial for creating reusable and well-structured Python programs, especially when dealing with modules and scripts.
The Basics of `__name__`
When a Python file is executed directly (e.g., python my_script.py
), the __name__
variable is set to '__main__'
. However, when a Python file is imported as a module into another file, __name__
is set to the name of the module (i.e., the filename without the .py
extension).
Demonstrating `__name__`
This example shows how to use __name__
. If you run the script directly, it will print 'This script is being run directly' and then 'Function executed!'. If you import this script into another script, it will print 'This script is being imported as a module'.
def my_function():
print("Function executed!")
if __name__ == "__main__":
print("This script is being run directly")
my_function()
else:
print("This script is being imported as a module")
Concepts Behind the Snippet
The key concept here is conditional execution. We use an if
statement to check the value of __name__
. This allows us to define code that should only run when the script is executed directly, and separate it from code that defines functions or classes intended to be used when the script is imported as a module.
Real-Life Use Case Section
A common use case is to define a set of functions or classes in a module, and then include a section of code that runs some tests or provides a command-line interface only when the module is executed directly. This makes the module both reusable and executable. For example, a module containing image processing functions could include a __main__
block that loads an image, applies the functions, and displays the result.
Best Practices
__name__ == '__main__'
block in your scripts if you intend them to be both runnable and importable.__main__
block to contain example usage, unit tests, or a command-line interface.__main__
block, as it will be executed whenever the module is imported, which might not be the desired behavior.
Interview Tip
When asked about __name__
in an interview, be sure to explain its purpose and how it is used to differentiate between a script being executed directly and being imported as a module. Providing a code example will solidify your understanding.
When to use them
Use the if __name__ == "__main__":
block whenever you want to add test functions or scripts or functions that should run locally (like a debugging tool) without affecting the main functions of the module
Alternatives
There aren't really 'alternatives' to using However, for very simple scripts, you could avoid using it, but it's generally good practice to include it, even if just as a placeholder.__name__
to control execution flow based on how a module is being used. It's the standard and recommended way to achieve this in Python.
Pros
Cons
There are no significant cons to using __name__
. It's a fundamental part of Python's module system.
FAQ
-
What happens if I don't use `if __name__ == '__main__':`?
If you don't use the
if __name__ == '__main__':
block, any code outside of function or class definitions will be executed whenever the module is imported. This can lead to unexpected behavior, especially if the code performs actions that should only be done when the script is run directly. -
Can I change the value of `__name__`?
No, you cannot directly change the value of
__name__
. It is a special built-in variable that is automatically set by the Python interpreter. -
Is `__name__` a constant?
No,
__name__
is not a constant. Its value depends on how the module is being used (directly executed or imported).