How to tell if I'm being run from a shell or a module

dg.google.groups at thesamovar.net dg.google.groups at thesamovar.net
Thu Feb 14 17:45:03 EST 2008


On Feb 14, 11:06 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> It depends on what you mean by "an interactive shell"? If you start your
> script with:
> python -i whatever.py
> is it an interactive shell or not?
>
> I tried these two criteria:
> a) See if the __main__ module has a __file__ attribute.
> b) See if sys.stdin is a real tty

Right, so my idea of an 'interactive shell' seems to be a little ill
defined. Nonetheless, looking if the main module has a file attribute
looked to be a good idea until I tried it on IPython - returns True...

I think I'll do what I said in the first post, but add an option to
override the behaviour for less usual circumstances. Seems to be the
best compromise.

Dan



More information about the Python-list mailing list