Interview Questions

How do I find the current module name?

Python Questions and Answers


(Continued from previous question...)

How do I find the current module name?

A module can find out its own module name by looking at the predefined global variable How do I find the current module name?. If this has the value '__main__', the program is running as a script. Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking How do I find the current module name?:

def main():
print 'Running test...'
...

if How do I find the current module name? == '__main__':
main()

(Continued on next question...)

Other Interview Questions