if __name__ == 'main':

Discussion in 'Python' started by gtb, Mar 20, 2007.

  1. gtb

    gtb Guest

    Hi,

    I often see the following 'if' construct in python code. What does
    this idiom accomplish? What happens if this is not main? How did I get
    here if it is not main?

    Thanks,

    gtb

    ==============================================

    if __name__ == 'main':
    myQuest('myQuest').Run()
    gtb, Mar 20, 2007
    #1
    1. Advertising

  2. On 2007-03-20, gtb <> wrote:

    > I often see the following 'if' construct in python code. What does
    > this idiom accomplish?


    It checks to see if the file is being run as the "main"
    program, and does something if that is so.

    > What happens if this is not main?


    Nothing.

    > How did I get here if it is not main?


    By importing the file.

    > if __name__ == 'main':
    > myQuest('myQuest').Run()
    >



    --
    Grant Edwards grante Yow! Thank god!!... It's
    at HENNY YOUNGMAN!!
    visi.com
    Grant Edwards, Mar 20, 2007
    #2
    1. Advertising

  3. gtb wrote:


    > I often see the following 'if' construct in python code. What does
    > this idiom accomplish? What happens if this is not main? How did I get
    > here if it is not main?
    > ...
    > if __name__ == 'main':
    > myQuest('myQuest').Run()


    This idiom is for executing the code if you're running the .py directly,
    or doing nothing if you're just "import"ing the module.

    Take note, that when you import a module, __name__ gets the module name.

    Regards,

    --
    .. Facundo
    ..
    Blog: http://www.taniquetil.com.ar/plog/
    PyAr: http://www.python.org/ar/
    Facundo Batista, Mar 20, 2007
    #3
  4. gtb

    Patrick Down Guest

    On Mar 20, 11:49 am, "gtb" <> wrote:
    > Hi,
    >
    > I often see the following 'if' construct in python code. What does
    > this idiom accomplish? What happens if this is not main? How did I get
    > here if it is not main?


    A quick example demonstrates the usage:

    C:\code>type temp.py


    print "Module name is",__name__

    if __name__ == "__main__":
    print "I was not imported"
    else:
    print "I was imported"

    C:\code>python temp.py
    Module name is __main__
    I was not imported

    C:\code>python
    Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
    on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import temp

    Module name is temp
    I was imported
    Patrick Down, Mar 20, 2007
    #4
  5. gtb

    gtb Guest

    On Mar 20, 12:13 pm, "Patrick Down" <> wrote:
    > On Mar 20, 11:49 am, "gtb" <> wrote:
    >
    > > Hi,

    >
    > > I often see the following 'if' construct in python code. What does
    > > this idiom accomplish? What happens if this is not main? How did I get
    > > here if it is not main?

    >
    > A quick example demonstrates the usage:
    >
    > C:\code>type temp.py
    >
    > print "Module name is",__name__
    >
    > if __name__ == "__main__":
    > print "I was not imported"
    > else:
    > print "I was imported"
    >
    > C:\code>python temp.py
    > Module name is __main__
    > I was not imported
    >
    > C:\code>python
    > Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
    > on win32
    > Type "help", "copyright", "credits" or "license" for more information.>>> import temp
    >
    > Module name is temp
    > I was imported



    Thanks, all! Makes great sense.


    Teas all 'round the canteen now,

    gtb
    gtb, Mar 20, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amy G
    Replies:
    4
    Views:
    375
    Amy G
    Feb 3, 2004
  2. Hakan Nilsson
    Replies:
    1
    Views:
    314
    Mirko Zeibig
    Feb 3, 2004
  3. Eli Stevens \(WG.c\)

    if (__name__ == '__main__'): main(sys.argv[1:])

    Eli Stevens \(WG.c\), Apr 20, 2004, in forum: Python
    Replies:
    1
    Views:
    887
    Peter Hansen
    Apr 20, 2004
  4. =?iso-8859-1?Q?Fran=E7ois?= Pinard

    Re: if (__name__ == '__main__'): main(sys.argv[1:])

    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Apr 21, 2004, in forum: Python
    Replies:
    7
    Views:
    993
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Apr 23, 2004
  5. gtb
    Replies:
    8
    Views:
    365
    Steven D'Aprano
    May 1, 2007
Loading...

Share This Page