Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61574

Re: Is there any advantage to using a main() in python scripts?

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Is there any advantage to using a main() in python scripts?
Date 2013-12-11 09:17 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-6DF7ED.09173411122013@news.panix.com> (permalink)
References <32615c9a-b983-4399-bb55-6df6c230f247@googlegroups.com>

Show all headers | View raw


In article <32615c9a-b983-4399-bb55-6df6c230f247@googlegroups.com>,
 JL <lightaiyee@gmail.com> wrote:

> Python scripts can run without a main(). What is the advantage to using a 
> main()? Is it necessary to use a main() when the script uses command line 
> arguments? (See script below)
> 
> #!/usr/bin/python
> 
> import sys
> 
> def main():
>     # print command line arguments
>     for arg in sys.argv[1:]:
>         print arg
> 
> if __name__ == "__main__":
>     main()

No, it's not necessary, but it's a good idea.

For one thing, it lets you import your script without actually running 
it.  We recently tracked down a long-standing bug where some maintenance 
script we had started out with:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'whatever'

somebody imported that script in another part of the system because 
there was some convenient definition that he wanted to reuse.  
Unfortunately, the act of importing the script changed the environment!

The fix was to move the setting of the environment variable to inside 
the main() routine.  If you always follow the rule that you always put 
all your executable code inside main(), you'll never run into problems 
like that.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is there any advantage to using a main() in python scripts? JL <lightaiyee@gmail.com> - 2013-12-11 01:55 -0800
  Re: Is there any advantage to using a main() in python scripts? Ben Finney <ben+python@benfinney.id.au> - 2013-12-11 21:26 +1100
  Re: Is there any advantage to using a main() in python scripts? Chris Angelico <rosuav@gmail.com> - 2013-12-11 21:41 +1100
  Re: Is there any advantage to using a main() in python scripts? marduk@letterboxes.org - 2013-12-11 07:57 -0500
    Re: Is there any advantage to using a main() in python scripts? Roy Smith <roy@panix.com> - 2013-12-11 09:20 -0500
  Re: Is there any advantage to using a main() in python scripts? Roy Smith <roy@panix.com> - 2013-12-11 09:17 -0500
    Re: Is there any advantage to using a main() in python scripts? rusi <rustompmody@gmail.com> - 2013-12-11 06:24 -0800
  Re: Is there any advantage to using a main() in python scripts? bob gailer <bgailer@gmail.com> - 2013-12-11 10:42 -0500
  Re: Is there any advantage to using a main() in python scripts? Chris Angelico <rosuav@gmail.com> - 2013-12-12 02:49 +1100
  Re: Is there any advantage to using a main() in python scripts? Terry Reedy <tjreedy@udel.edu> - 2013-12-11 16:22 -0500

csiph-web