[Tutor] Which is better Practice and why

Devin Jeanpierre jeanpierreda at gmail.com
Tue Oct 23 03:53:08 CEST 2012


On Mon, Oct 22, 2012 at 8:54 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> Recently I've become a fan of executable packages. In __main__.py,
>> it's always the right thing to do.
>
>
> I would disagree there too. I think that unconditionally running main
> is the wrong thing to do, except perhaps in the most trivial quick-and-
> dirty scripts. But if the script it that trivial, you might not even
> bother with a main function at all.
>
> package.__main__.py is designed to be run as a script, and not to be
> imported. But that doesn't mean that there's no good purpose for
> importing it. If your package is non-trivial, you ought to have tests
> for it, including package.__main__. Those tests will probably want to
> import the module, not necessarily run it as a script.

I think you will find that it is never the case that __name__ !=
'__main__' in a file called "__main__.py".

If I want to test something, I put it in another file. My __main__.py
file will generally look something like this:

    from . import game
    game.Game().run()

-- Devin


More information about the Tutor mailing list