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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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 Case

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/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

Issue

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

1
2
3
4
5
6
7
8
9
#!/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.

Solution

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/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 unrelated 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 imports it.

All done!


Changelog

  • 2021-05-02 : Fixed typo and improve subtitles