Have you ever skimmed through a Python codebase with different modules? If yes, you’d probably have come across if name == ’main’ conditional in one or more modules. Over the next few minutes, we’ll demystify what the above conditional means and look at an example where it can be helpful. Let’s begin!

What is the Significance of name in Python?

In Python, a module is a .py file that contains function definitions, a set of expressions to be evaluated, and more. For example, if  we have a file named hello_world.py, we refer to this as the hello_world.py file or the hello_world module. When you run a Python module, the Python interpreter sets the values for a few special variables ahead of execution: name is one of them. The key to understanding the significance   name is understanding how imports work in Python. Head over to the folder example-1. We have the file module1.py. The name variable is in the namespace of the current module. This module prints out a line followed by the value of the name variable. Now, let’s run module1 from the command line. In the output, we see that the name variable is set to main.

Importing Modules in Python

In addition to running a Python module, you may sometimes want to use functionality from another Python module inside the current module. Python facilitates this through imports. Imports let you reuse the functionality of another module—by importing it into the current module’s scope—without having to rewrite the code. The module2.py file contains the following. We have imported module1 inside. module2. We run module2.py and observe the output. In the output below:

We see that module1 is run under the hood when we import it inside module2, and the corresponding output is printed out. But this time, the name variable is not main but module1. Because we ran module2 directly, the name variable corresponding to the module is now main.

Example of if name==’main’ in Python

In the section, we’ll see a practical use case of the if name == ‘main’ conditional. We’ll define a simple function and then write unit tests to check if the function is working as expected. – If a module  is run directly, its name variable is set to is equal to main.  – If a module is imported inside another module, its name is set to the name of the module. The code for this section can be found in the example-2 folder. Here, add.py is a Python file that contains the definition of the function add_ab().The function add_ab() takes in any two numbers and returns their sum. We’ll use Python’s unittest module to test the function add_ab().

Writing Test Cases for a Python Function

Look at the code snippet below, containing the contents of the test_add module. The above code does the following:

Imports Python’s built-in unittest moduleImports the function add_ab() from the add moduleDefines the test class TestAdd and a set of test cases as methods within the test class

To set up unit tests for your code, you should first define a test class that inherits from unittest.TestCase. All test cases should be specified as methods inside the class and should start with test_. Now let’s try running the test_add module from the terminal. You’ll see that there is no output, and none of the tests have run. Why is this the case?🤔 This is because to run the unit tests, you should run unittest as the main module while running test_add.py, using the command below. Upon running the above verbose command, we see that all three tests have run successfully. However, it will be convenient to run the tests when this module test_add is run, yes? Let’s learn how to do it in the next section.

Using if name == ‘main’ to Run unittest as the Main Module

If you’d like to run all the unit tests when the module runs directly, you can add the conditional. The conditional in the above code snippet tells the Python interpreter: If this module is run directly, then run the code inside. unittest.main(). You can run the test_add module after adding the above two lines of code. ▶️ Running the test add module directly now runs all the three tests we’ve defined. The above output OK indicates that all the tests were run successfully. The three dots … indicate that three tests were run, and all passed. Now, let us change the expected return value test_add_1_minus7 to 8. Because the function returns – 6 in this case, there should be one failing test. As seen in the output below, we get .F., of the three tests, pattern one of them failed (the second test), and in the traceback, we get a AssertionError stating – 6 != 8. One important thing to note is that the tests do not necessarily run in the same order in which they are specified in the test class. In the above example, test_add_1_minus7 is defined as the third method in the test class, but the corresponding test was run second.

Summing Up

I hope this tutorial helped you understand how the if name == ‘main’ conditional works in Python.  Here’s a quick recap of the key takeaways:

The Python interpreter sets the name variable before executing the Python script.When you run a module directly, the value of the name is main.When you import a module inside another Python script, the value of the name is the module name.You can use if name == ‘main’ to control execution and which parts of the module run during direct and imported runs, respectively.

Next, check out this in-depth guide on Python sets. Happy learning!🎉

Understanding if   name         main    in Python - 27Understanding if   name         main    in Python - 79Understanding if   name         main    in Python - 58Understanding if   name         main    in Python - 52Understanding if   name         main    in Python - 84Understanding if   name         main    in Python - 39Understanding if   name         main    in Python - 21Understanding if   name         main    in Python - 71Understanding if   name         main    in Python - 18Understanding if   name         main    in Python - 60Understanding if   name         main    in Python - 92Understanding if   name         main    in Python - 99Understanding if   name         main    in Python - 63Understanding if   name         main    in Python - 27Understanding if   name         main    in Python - 47Understanding if   name         main    in Python - 92Understanding if   name         main    in Python - 15Understanding if   name         main    in Python - 11Understanding if   name         main    in Python - 11Understanding if   name         main    in Python - 68Understanding if   name         main    in Python - 72Understanding if   name         main    in Python - 16Understanding if   name         main    in Python - 2Understanding if   name         main    in Python - 91Understanding if   name         main    in Python - 49Understanding if   name         main    in Python - 70Understanding if   name         main    in Python - 16Understanding if   name         main    in Python - 47