Re: Restart the interactive python shell like in IDLE

Discussion in 'Python' started by Terry Reedy, Jun 11, 2009.

  1. Terry Reedy

    Terry Reedy Guest

    Chris Rebert wrote:
    > On Wed, Jun 10, 2009 at 12:01 PM, Matt Burson<> wrote:
    >> Is there a way to reproduce the behavior of IDLE's restart shell ability by
    >> using a function? I thought there would be since you can exit python by
    >> executing the simple quit() function I thought there would be an equally
    >> simple function name something like restart(). I'd prefer something like
    >> this as opposed to having to exit the shell and then start it up again to
    >> refresh it.

    >
    > I believe IDLE itself implements the "restart" capability by killing
    > and re-launching its Python interpreter subprocess, so it's not like
    > it's using some hidden capability of Python to accomplish this.
    > Is doing Ctrl+D, up-arrow, Enter really that hard? It's even fewer
    > keystrokes than "restart()"...


    This will do part of what you want:

    >>> a=1
    >>> b=1
    >>> globals().clear()
    >>> a

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'a' is not defined

    That will not reset sys.modules, which is the only other thing I can
    imagine being worried about.

    The main reason IDLE has a restart is so that when you run a file after
    editing, you can be sure the behavior you see is what you get when
    running the file without IDLE, with a fresh interpreter. Another use of
    refresh is when creating example interactive sessions for doctest or
    book examples. Again, one wants to make sure that the example does not
    depend on previous entries not included in the example. For ordinary
    interactive exploration, refresh is seldom needed.

    tjr
    Terry Reedy, Jun 11, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ashot
    Replies:
    22
    Views:
    4,182
    Fernando Perez
    Feb 8, 2005
  2. markscottwright

    How to I restart an interactive session?

    markscottwright, Mar 18, 2005, in forum: Python
    Replies:
    4
    Views:
    339
    Diez B. Roggisch
    Mar 19, 2005
  3. Chris Rebert
    Replies:
    1
    Views:
    1,114
    Ricardo Abreu
    Sep 30, 2013
  4. lzlu123
    Replies:
    4
    Views:
    327
    lzlu123
    Aug 22, 2011
  5. Josef Moellers

    Restart Perl Application upon KDE Restart

    Josef Moellers, Jul 18, 2013, in forum: Perl Misc
    Replies:
    18
    Views:
    253
    Adam H. Kerman
    Jul 26, 2013
Loading...

Share This Page