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

alex23 wuwei23 at
Thu Jul 16 06:14:37 CEST 2009

On Jul 16, 12:56 pm, Asun Friere <afri... at> wrote:
> As you note there, this will work when running the vanilla shell (ie
> running  it from the command line), but not (potentially) in other
> interactive environments (IronPython being the example you give).

Actually, that was IPython, which is an enhanced interactive prompt,
and a totally different beastie altogether :)

> Another instance:  there is not sys.ps1 when running a python shell
> under idle.  Since this solution differs whether the interactive
> session is taking place from the cmd line, idle, IronPython etc. it
> seems to me not terribly robust.

Well, none of {idle, IronPython, IPython} were specified by the OP AND
you're citing back what I myself wrote in the link to which I also
referred the OP - whom 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

> 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__'

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

More information about the Python-list mailing list