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

2008-02-15 Thread Sion Arrowsmith
Gabriel Genellina <[EMAIL PROTECTED]> wrote: >a) See if the __main__ module has a __file__ attribute. >b) See if sys.stdin is a real tty c) See if sys.argv[0] != '' (Although this works for the command line interactive shell, I've a suspicion it will fail with IDLE. But I don't have IDLE to hand

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

2008-02-14 Thread dg . google . groups
On Feb 14, 11:06 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> 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__ a

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

2008-02-14 Thread Gabriel Genellina
En Thu, 14 Feb 2008 19:09:10 -0200, <[EMAIL PROTECTED]> escribió: > Thanks for the replies, but it's not what I meant. What I want to be > able to determine is whether or not the user is running from an > interactive shell (like IPython or IDLE). Checking if > __name__=='__main__' checks if the

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

2008-02-14 Thread dg . google . groups
Thanks for the replies, but it's not what I meant. What I want to be able to determine is whether or not the user is running from an interactive shell (like IPython or IDLE). Checking if __name__=='__main__' checks if the current module is the one being run, but suppose you have two modules A and B

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

2008-02-14 Thread Chris
On Feb 14, 7:21 pm, [EMAIL PROTECTED] wrote: > Hi all, > > Is there any standard way to tell if the user is running from a module > or from an interactive shell like IDLE or IPython? The best I've come > up with so far is for a function to look at > getouterframes(currentframe())[1][1] (the filenam

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

2008-02-14 Thread Jeff McNeil
Check to see what the value of '__name__' is, for example: if __name__ == '__main__': execute_interactive_code() else: I_am_just_a_lowly_module() The value of __name__ will correspond to the name of your module: $ cat a.py print __name__ $ $ python Python 2.5.1 (r251:54863, Oct 30 2007, 1

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

2008-02-14 Thread Jeff
Conventionally, you use: if __name__ == '__main__': # do something as a script -- http://mail.python.org/mailman/listinfo/python-list

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

2008-02-14 Thread dg . google . groups
Hi all, Is there any standard way to tell if the user is running from a module or from an interactive shell like IDLE or IPython? The best I've come up with so far is for a function to look at getouterframes(currentframe())[1][1] (the filename in the frame record of the frame that called the funct