Check to see what the value of '__name__' is, for example:<br><br>if __name__ == '__main__':<br> execute_interactive_code()<br>else:<br> I_am_just_a_lowly_module()<br><br>The value of __name__ will correspond to the name of your module:<br>
<br>$ cat a.py<br>print __name__<br>$ <br><br>$ python<br>Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11) <br>[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2<br>Type "help", "copyright", "credits" or "license" for more information.<br>
>>> import a<br>a<br>>>> <br><br>Thanks!<br><span class="sg"><br>Jeff</span><br><br><div><span class="gmail_quote">On 2/14/08, <b class="gmail_sendername"><a href="mailto:dg.google.groups@thesamovar.net">dg.google.groups@thesamovar.net</a></b> <<a href="mailto:dg.google.groups@thesamovar.net">dg.google.groups@thesamovar.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br> <br> Is there any standard way to tell if the user is running from a module<br> or from an interactive shell like IDLE or IPython? The best I've come<br> up with so far is for a function to look at<br> getouterframes(currentframe())[1][1] (the filename in the frame record<br>
of the frame that called the function), and check if it exists or not<br> with os.path.exists. IPython gives '(ipython console)' and IDLE gives<br> 'pyshell#0' whereas running from a module gives its filename. This<br>
seems a bit hacky. Any better ideas?<br> <br> --<br> Dan Goodman<br> <a href="http://thesamovar.net/contact">http://thesamovar.net/contact</a><br> <br>--<br> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
</blockquote></div><br>