Can module tell if running from interpreter vs Windows command line ?

Asun Friere afriere at yahoo.co.uk
Thu Jul 16 07:44:40 CEST 2009


On Jul 16, 2:14 pm, alex23 <wuwe... at gmail.com> wrote:

...

> AND
> you're citing back what I myself wrote in the link to which I also
> referred the OP - whom (sic) has subsequently posted his success with this
> technique - so I'm not really sure what the point is here other than
> "if you have a different use case, you'll need a different
> solution"...
>

Please don't take offence where none was intended.  I know that I was
citing you and that I was highlighting the caveat you raised,
explicitly so.  No criticism of your post was intended nor implied.
Moreover the fact that it worked for OP on a single occasion does not
speak for its robustness.

> > Depending on the use case, it is of course easy to tell whether the
> > module was executed on the command line, or imported (from an
> > interactive shell or another script) using the __name__ trick.  (eg.
> > is_imported = __name__ == '__main__')
>
> That should be:
>
> is_imported = __name__ != '__main__'
>

Doh! ... Yup I actually used that when I tried it out, my bad.

> And such a test is all well and good if the main code body is the one
> that needs to know about the execution mode, but if you need to know
> under which conditions the program is being run within a module
> imported by the main body, well, that check is _always_ going to be
> true...

Which is what the paragraph you just quoted says.  Hence the attribute
is called 'is_imported' rather that 'running_non_interactively'.



More information about the Python-list mailing list