[Python] Leveraging __name__ for Efficient Python Module Organization

[Python] Leveraging __name__ for Efficient Python Module Organization

Introduction

If you have used python before, you may have seen a code snippet like this:

if __name__ == '__main__':
    # do something

This code is so simple and short, but do you fully understand what it means, and why it is so important to avoid errors when you create modules? This article will introduce what __name__ is, and how essential it is when we create modules.


What is __name__?

You may have seen some python codes that start with double underscore, which we also call dunder (short for “double underscore”). It is a special or built-in variable. __name__ is assigned with a new value when python interpreter runs. Therefore, it is a variable, but you don’t declare and you don’t really assign a value to it. It automatically works as python interpreter runs.

The value of __name__ is dependent on whether your current file is imported or not. Let’s have a look at this example:

# ex_module.py
def func():
    print("Hi")

print('__name__:', __name__)
# main.py
import ex_module.py # IMPORTANT! ex_module is imported, so __name__ changes.
ex_module.func()

Output:

>>> Hi 
>>> __name__: ex_module

__name__ is ex_module because ex_module.py is imported in main.py.

Now, what if ex_module.py is not imported?

# ex_module.py
print('__name__:', __name__)

Output:

>>> __name__: __main__

__name__ is __main__ because ex_module.py is not imported anywhere else. Consequently, this means ex_module.py is the main file. Let’s say you are building a program, and you will need a lot of modules to import. You would have built your custom modules, right? The main file imports all the modules you created, but is the main file imported? → NO!

So, python interpreter understands that it is the main file if the file is not imported.


Why Do We Need __name__?

We can distinguish between running directly and importing as a module. When a Python file is executed directly, the interpreter sets the special variable __name__ to "__main__". If the file is imported as a module, __name__ will be set to the name of the module.

This behavior is particularly useful for writing scripts that can be used both as standalone programs and as reusable modules.

Did you find this article valuable?

Support Jay's Dev Blog by becoming a sponsor. Any amount is appreciated!