An introduction to the __name__ variable

You probably seen the __name__ variable if you’ve gone through some python open source code. For instance below:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import os
from logging import FileHandler
from logging import Formatter

from flask import Flask
from flask import render_template
from flask import request
from forms import *

app = Flask(__name__)
app.config.from_object("config")


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/login")
def login():
    form = LoginForm(request.form)
    return render_template("forms/login.html", form=form)


@app.route("/register")
def register():
    form = RegisterForm(request.form)
    return render_template("forms/register.html", form=form)


@app.errorhandler(404)
def not_found_error(error):
    return render_template("errors/404.html"), 404


if not app.debug:
    file_handler = FileHandler("error.log")
    file_handler.setFormatter(
        Formatter("%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]")
    )
    app.logger.setLevel(logging.INFO)
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)
    app.logger.info("errors")


if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host="127.0.0.1", port=port)

Whenever the python interpreter reads a source file, it does two things:

  • sets a few special/global variables like __name__, then;
  • executes all of the code found in the file.

Why is the __name__ variable used?

The __name__ is a special python variable. It gets it’s value depending on how we execute the containing module.

Sometimes you write a module with functions that might be useful in other module as well. In python, you can import that module into another module.

Thanks to this special variable, you can decide whether you want to run the the whole module. Or you want to import the functions defined in the module.

To grasp the context more vividly let see some use case examples.

Use Cases

Let’s assume that we have module which has a simple function called get_max() to get the maximum integer in a given array:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# module_one.py


def get_max(array: list) -> int:
    return max(array)


print("Hello Module 1")
array_example = [9, 0, 1, -6, 3]
# Calling the function
print(f"max int: {get_max(array=array_example)}")

if we run this module we get the below result:

$ python module_one.py
Hello Module 1
max int: 9

Case 1

So now assume that we need this get_max() function from other module that we write:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# module_two.py
import module_one


print("Hello Module 2")
array_example = [3, 17, -28, 4]
print(f"max int: {module_one.get_max(array=array_example)}")

If we now run the module_two.py we get the output:

$ python module_two.py
Hello Module 1
max int: 9
Hello Module 2
max int: 17

According to our program, the output should be “17” because the only get_max() function is called. But the whole module is imported.

Case 2

To overcome this we use if __name__ == “__main__”. The extra line of codes written after function get_max() in module_one.py is kept inside of it so it won’t be executed while the function is imported in module_two.py.

Now update our module_one.py like below:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# module_one.py


def get_max(array: list) -> int:
    return max(array)


if __name__ == "__main__":
    print("Hello Module 1")
    array_example = [9, 0, 1, -6, 3]
    # Calling the function
    print(f"max int: {get_max(array=array_example)}")

Now run the module_two.py:

$ python module_two.py
Hello Module 2
max int: 17

You see that after using if __name__ == “__main__” the unreletad codes will not be used by module_two.py.

Conclusion

After all that we understand that what does if __name__ == “__main__” do in python; it prevents certain code to run if any other file import it.

All done!