Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Dec 182010
 

How do I find the current module name in Python?

Answer:

The easiest way is to look at the look at the predefined global variable __main__. If it has the value "__main__", it means the program is run as a script (not as imported module).

E.g.

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


if __name__ == '__main__':
    main()

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>