Can module tell if running from interpreter vs Windows command line ?
afriere at yahoo.co.uk
Thu Jul 16 04:56:38 CEST 2009
On Jul 16, 10:47 am, alex23 <wuwe... at gmail.com> wrote:
> This older post should help:http://groups.google.com/group/comp.lang.python/browse_frm/thread/6c5...
> But the quick answer is to import sys into your program, and do a test
> on hasattr(sys, 'ps1'), which is only created when running the
> interactive prompt.
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).
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.
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__')
More information about the Python-list