What does if __name__ == “__main__”: do?

A module’s __name__

Every module has a name and statements in a module can find out the name of its module. This is especially handy in one particular situation – As mentioned previously, when a module is imported for the first time, the main block in that module is run. What if we want to run the block only if the program was used by itself and not when it was imported from another module? This can be achieved using the __name__ attribute of the module.

Using a module’s __name__

#!/usr/bin/python

# Filename: using_name.py

if __name__ == '__main__':
    print 'This program is being run by itself'
else:
    print 'I am being imported from another module'

Output

                
$ python using_name.py
This program is being run by itself

$ python
>>> import using_name
I am being imported from another module
>>>

How It Works

Every Python module has it’s __name__ defined and if this is '__main__', it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.


Question:

What does the if __name__ == "__main__": do?

# Threading example
import time, thread

def myfunction(string, sleeptime, lock, *args):
while 1:
lock
.acquire()
time
.sleep(sleeptime)
lock
.release()
time
.sleep(sleeptime)
if __name__ == “__main__”:
lock
= thread.allocate_lock()
thread
.start_new_thread(myfunction, (“Thread #: 1”, 2, lock))
thread
.start_new_thread(myfunction, (“Thread #: 2”, 2, lock))

Answer:

When the Python interpreter reads a source file, it executes all of the code found in it.

Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module’s name.

In the case of your script, let’s assume that it’s executing as the main function, e.g. you said something like

python threading_example.py

on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that __name__ does equal "__main__", so it will execute the block shown there.

One reason for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.


Below is a similar Ruby feature

if __FILE__ == $0
  do_something
end

__FILE__ is the magic variable that contains the name of the current file. $0 is the name of the file used to start the program. This check says “If this is the main file being used…” This allows a file to be used as a library, and not to execute code in that context, but if the file is being used as an executable, then execute that code.