__name__ (A Special variable) in Python

Python does not require a main function to start execution like many other programming languages. Instead, it uses a special built-in variable called __name__ to determine how a Python script is being executed (directly or imported as a module into another script).

In this article, we will learn about the __name__ variable in Python and how to use it effectively.

Understanding the __name__ Variable

The __name__ variable is a built-in variable that holds the name of the current module.

  • When you run a script directly, Python sets __name__ to __main__.
  • If the same script is imported into another file as a module, __name__ is set to the name of that module.

This is useful when we want to use or test functions, variables, or classes of a module without executing its main code.

Basic Example

Let us look at a simple example that shows the type and value of __name__ ?

print(type(__name__))
print(__name__)

The output of the above code is ?

<class 'str'>
__main__

Here, we see that __name__ is a string, and its value is __main__ when the script is executed directly.

Running a Stand-alone Script

In this example, since the script is run directly, the condition if __name__ == '__main__' evaluates to True, so func1() is called ?

def func1():
    print('The value of __name__ is ' + __name__)

if __name__ == '__main__':
    func1()

The output of the above code is ?

The value of __name__ is __main__

Importing a Module Example

When a module is imported, the __name__ variable contains the module's name instead of __main__. Here's how it works ?

# File: my_module.py
def func1():
    print('The value of __name__ is ' + __name__)

if __name__ == '__main__':
    func1()

Now, create another script and import the previous file ?

# File: main_script.py
import my_module as mod

print('Running the imported function')
mod.func1()

print('\nRunning the current script')
print('The value of __name__ is ' + __name__)

When you run main_script.py, the output would be ?

Running the imported function
The value of __name__ is my_module

Running the current script
The value of __name__ is __main__

Common Use Cases

The if __name__ == "__main__" pattern is commonly used for ?

  • Testing code: Run test cases only when the script is executed directly
  • Demo functions: Show examples of how to use the module's functions
  • Command-line tools: Create scripts that work as both modules and standalone programs

Practical Example

Here's a practical example showing a calculator module ?

def add(a, b):
    return a + b

def multiply(a, b):
    return a * b

# This code runs only when script is executed directly
if __name__ == '__main__':
    # Test the functions
    result1 = add(5, 3)
    result2 = multiply(4, 7)
    print(f"5 + 3 = {result1}")
    print(f"4 × 7 = {result2}")

The output when running directly ?

5 + 3 = 8
4 × 7 = 28

When imported as a module, only the functions are available, but the test code doesn't run automatically.

Conclusion

The __name__ special variable allows you to create Python scripts that can function both as standalone programs and importable modules. Use if __name__ == "__main__" to control which code runs only when the script is executed directly.

Updated on: 2026-03-25T06:06:05+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements