3

I understand that __main__ is the name of __main__.py, which in this case is test_employee. But what I don't understand is the unittest module and the class that I want to test are being imported. Then why __name__ is still same as __main__? As I understood, __name__ represents the modules being imported.

test_employee.py

import unittest

from employee import Employee

class TestEmployee(unittest.TestCase):

    def setUp(self):
        self.employee1 = Employee('June', 'July')
        self.employee2 = Employee('Jane', 'Marshal')
    
    def test_give_default_raise(self):
        self.assertEqual(5000, self.employee1.annual_salary)

    def test_give_custom_raise(self):
        self.employee2.give_raise(1000)
        self.assertEqual(6000, self.employee2.annual_salary)

if __name__ == '__main__':
    unittest.main()
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
Sean
  • 57
  • 1
  • 5
  • The module that is originally executed receives the name `__main__`. https://docs.python.org/3/library/__main__.html – Klaus D. Jun 22 '20 at 05:04

5 Answers5

3

__main__ is a very particular function of python. It has mainly two interests:

  • if __name__ == '__main__' condition
  • __main__.py file for a module (which is not the purpose hier)

When importing a module, the __name__ attribute of this module is set to the name of the module (i.e. the file name without the .py extension).

>>> import my_module
>>> my_module.__name__
'my_module'

>>> import my_module.submodule
>>> my_module.submodule.__name__
'my_module.submodule'

However, if the attribute is called from the main runtime environment (which is the main use case), then the variable will always contain the value '__main__'. The main purpose of this is to test whether the main script is called from the main runtime environment or not. This is usefull to differentiate the case where the module is called directly (for instance from command line) and the one where it is loaded from another module (to launch an argparse, for example...)

Nevertheless, it is recommended to put as little code as possible under the condition if __name__ == '__main__' to avoid errors.

It is recommended to do as follows:

# echo.py

import shlex
import sys

def echo(phrase: str) -> None:
   """A dummy wrapper around print."""
   # for demonstration purposes, you can imagine that there is some
   # valuable and reusable logic inside this function
   print(phrase)

def main() -> int:
    """Echo the input arguments to standard output"""
    phrase = shlex.join(sys.argv)
    echo(phrase)
    return 0

if __name__ == '__main__':
    sys.exit(main())
Kaz
  • 1,047
  • 8
  • 18
  • Pardon me, you mean that `__name__ ` is the name of the file without the `.py` extension? Like if my file name is `mycode.py` the __name__ here is mycode, and the condition needs `main` so how the code will run? I understood the first part, but facing some issue understanding the second part, sorry. – Omar The Dev Feb 05 '22 at 02:58
  • Oh yeah, I got your point but if the attribute is called not from the environment, so it depends on the Python File Name, right ? – Omar The Dev Feb 05 '22 at 03:02
  • Yes, if you are in e the "main file" (the one you call from command-line), `__name__` is equals to `"__main__"`. Otherwise, `__name__` is equals to the name of you python file (whitout extension) or module name. – Kaz Feb 06 '22 at 09:40
  • Please quote your sources. This is directly taken from the official documentation While helpful, it's only fair to quote where you got it from. – martin-martin Aug 11 '22 at 09:44
1

When you run your script, the __name__ variable equals __main__. When you import a script,__name__ will contain the name of the script.

One important reason to add if __name__ == '__main__': to you script is to make sure that this code is not executed when your script is imported from another script. Because in that case the __name__ will be the name of your script.

See this answer for more details https://stackoverflow.com/a/419185/6914378

So when you run test_employee.py:

  • __name__ in test_employee.py will be __main__
  • __name__ in employee.py will be employee
  • __name__ in unittest.py will be unittest

For more information you can have a look here: https://www.freecodecamp.org/news/whats-in-a-python-s-name-506262fe61e8/

  • I'm asking in a general question, explain the `__name__` and `__main__` , forget the test employee and these, it's something like a general question and answer. – Omar The Dev Feb 05 '22 at 04:14
0

Functions and classes that are defined, but none of their code runs. When You Use name == main its Executed when invoked directly as when its invoked directly name will be main.

Thiru17
  • 9
  • 4
0

You are running the test inside main module..not the Employee(which is being imported)..so when you setUp the unittest, the instance of Employee becomes the instant of TestEmployee class..Hope it was clear to you!!!

pritam samanta
  • 434
  • 4
  • 10
0

The __name__ and __main__ has been very well described in below Python documentation:

https://docs.python.org/3/library/__main__.html#:~:text=%C2%B6,entry%20point%20to%20the%20application.

kwick
  • 333
  • 3
  • 9
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32787065) – rtoijala Sep 29 '22 at 12:53
  • That's a Good point. Thanks @rtoijala – kwick Oct 04 '22 at 08:54