Question about 'if __name__ == "__main__":'

Amy G amy-g-art at cox.net
Tue Feb 3 03:40:43 EST 2004


Thanks.  I think I will probably use the len(sys.args) line.  That seems
like a really staright forward approach since I will either be supplying an
argument - or not.


"Amy G" <amy-g-art at cox.net> wrote in message
news:k9ITb.28697$P17.28414 at fed1read03...
> I have a program that needs a little help.
> Right now the program runs in my crontab.  When it runs, it sets a few
> variables based on a query to a MySQL database.  I would like to modify it
> so that it can run as it is... or if arguments are supplied, use those
> instead of querrying the database.
>
> Will using this statement help me out?
>
> if __name__ == "__main__":
>
> I seem to recall that this returns true if it is run as a script by
python,
> rather than as a module from another prog.  Since I am going to run this
> from a command line - or from my crontab... it will always return true as
> far as I can tell.
>
> Any suggestions to help me out.
>
> Ultimately I want something like this pseudocode...
>
> if (no args supplied):
>     curs.execute("""SELECT userid FROM users""")
>     data = curs.fetchall()
> else:
>     data = sys.argv[1]
>
> do something with data:
>     blah
>     blah
>     blah
>
> Thanks in advance for your help.
>
>





More information about the Python-list mailing list