[Tutor] if __name__=='main' vs entry points: What to teach new comers?

Zachary Ware zachary.ware+pytut at gmail.com
Tue Aug 1 13:59:53 EDT 2017


On Tue, Aug 1, 2017 at 9:54 AM, Thomas Güttler
<guettliml at thomas-guettler.de> wrote:
> I have a friend who is a talented shell script writer. He is a linux guru
> since
> several years.
>
> He asked me if "if __name__=='main':" is state of the art if you want
> to translate a shell script to python.
>
> I started to stutter and did not know how to reply.

The ~~correct~~ pedantic answer is "No, it's `if __name__ == '__main__':`" :)

> I use Python since several years and I use console_script in entry_points of
> setup.py.
>
> I am very unsure if this is the right way if you want to teach a new comers
> the joy of python.
>
> In the current context we want to translate a bunch of shell scripts to
> python scripts.
>
> What do you think?

It depends on whether you're packaging them up as real Python packages
(sdist, wheel, etc.) to be installed in site-packages or similar, or
if they're just going to be standalone scripts (single file with a
shebang, possibly not even a `.py` extension).  If the former, go for
the entry points.  If the latter, go for `if __name__ == '__main__':`.
And there's no real reason not to do both in the former case anyway;
it's very convenient to not have to install your script somehow to be
able to run it.

-- 
Zach


More information about the Tutor mailing list