python shell that saves history of typed in commands that willpersist between reboots

Discussion in 'Python' started by goldtech, Nov 16, 2011.

  1. goldtech

    goldtech Guest

    Hi,

    Using Windows. Is there a python shell that has a history of typed in
    commands?

    I don't need output of commands just what I typed it. I need it to
    save between sessions - something that no shell seems to do. If I
    reboot there will still be a command history somewhere.

    Like bash history in Linux.

    Thanks
    goldtech, Nov 16, 2011
    #1
    1. Advertising

  2. goldtech

    sword Guest

    Maybe you're looking for ipython? History, tab-complete, sort of
    things in it.

    goldtech wrote:
    > Hi,
    >
    > Using Windows. Is there a python shell that has a history of typed in
    > commands?
    >
    > I don't need output of commands just what I typed it. I need it to
    > save between sessions - something that no shell seems to do. If I
    > reboot there will still be a command history somewhere.
    >
    > Like bash history in Linux.
    >
    > Thanks
    sword, Nov 16, 2011
    #2
    1. Advertising

  3. goldtech

    Tim Golden Guest

    On 16/11/2011 03:38, goldtech wrote:
    > Hi,
    >
    > Using Windows. Is there a python shell that has a history of typed in
    > commands?


    Have a look at DreamPie:

    http://dreampie.sourceforge.net/

    TJG
    Tim Golden, Nov 16, 2011
    #3
  4. On Wed, Nov 16, 2011 at 4:09 PM, Ben Finney <> wrote:
    > goldtech <> writes:
    >
    >> Using Windows. Is there a python shell that has a history of typed in
    >> commands?

    >
    > I don't know about MS Windows, but the Python interactive shell can be
    > linked with the GNU Readline library for managing its command line
    > <URL:http://docs.python.org/library/readline.html> including editing
    > features, tab completion, history management, and a persistent history
    > file.
    >
    > You can then use that functionality in your Python interactive startup
    > file. Here's mine:
    >
    > =====
    > # $HOME/.pythonrc
    > # User configuration for interactive Python shell.
    >
    > import sys
    > import os
    > import os.path
    > import atexit
    >
    > # Tab completion with readline.
    > # Cribbed from <URL:http://docs.python.org/lib/module-rlcompleter.html>.
    > try:
    >    import readline
    > except ImportError:
    >    sys.stderr.write("Module readline not available.\n")
    > else:
    >    import rlcompleter
    >
    >    # Enable tab completion.
    >    readline.parse_and_bind("tab: complete")
    >
    >    # Persistent command history.
    >    histfile = os.path.join(os.environ["HOME"], ".python_history")
    >    try:
    >        readline.read_history_file(histfile)
    >    except IOError:
    >        # Existing history file can't be read.
    >        pass
    >    atexit.register(readline.write_history_file, histfile)
    >
    >    del histfile
    >
    > del sys, os, atexit
    >
    > =====
    >
    > Reading the documentation, I see that the ‘readline’ library is only
    > linked with Python on Unix-alike operating systems. Yet another reason
    > why MS Windows is not a good choice for developing software I guess.


    I'm not sure what documentation you're reading, but your code works fine on
    Windows. Thanks. [It is necessary to properly set PYTHONSTARTUP]
    David Robinow, Nov 16, 2011
    #4
  5. On Wed, Nov 16, 2011 at 6:59 PM, Ben Finney <> wrote:
    > David Robinow <> writes:
    >
    >> On Wed, Nov 16, 2011 at 4:09 PM, Ben Finney <>wrote:
    >> > I don't know about MS Windows, but the Python interactive shell can be
    >> > linked with the GNU Readline library for managing its command line
    >> > <URL:http://docs.python.org/library/readline.html>

    > […]
    >
    >> > Reading the documentation, I see that the ‘readline’ library is only
    >> > linked with Python on Unix-alike operating systems.

    >
    >>  I'm not sure what documentation you're reading

    >
    > The same documentation I linked to above. Immediately below the title,
    > it specifies a limited set of platforms: “Platforms: Unix” limiting the
    > availability of the described module.
    >
    >> but your code works fine on Windows. Thanks.

    >
    > I'm glad to know that. Perhaps you could investigate why, and suggest an
    > update to the above documentation if it's wrong? The bug tracker at
    > <URL:http://bugs.python.org/> would be the appropriate place for such a
    > suggestion.


    Upon further investigation, it turns out that I'm using pyreadline
    from http://pypi.python.org/pypi/pyreadline. I'd forgotten I'd
    installed it. No documentation fixes appear to be necessary.

    "The pyreadline package is a python implementation of GNU readline
    functionality it is based on the ctypes based UNC readline package by
    Gary Bishop. It is not complete. It has been tested for use with
    windows 2000 and windows xp."
    It appears to work in Vista also, at least for the purposes
    discussed in this thread.
    David Robinow, Nov 17, 2011
    #5
  6. goldtech

    alex23 Guest

    On Nov 17, 7:09 am, Ben Finney <> wrote:
    > You can then use that functionality in your Python interactive startup
    > file. Here's mine:


    Awesome, thank you for this. I use iPython where ever possible but
    there are times where I just can't avoid the default shell and this
    will help immensely.

    Cheers!
    alex23, Nov 17, 2011
    #6
  7. goldtech

    Anssi Saari Guest

    Re: python shell that saves history of typed in commands that will persist between reboots

    goldtech <> writes:

    > Using Windows. Is there a python shell that has a history of typed in
    > commands?


    Is there a shell that doesn't have history then? At least both the
    vanilla shell and Idle both have basic history in Windows. IPython for
    more fun.
    Anssi Saari, Nov 23, 2011
    #7
  8. On Wed, 23 Nov 2011 11:23:19 +0200, Anssi Saari wrote:

    > goldtech <> writes:
    >
    >> Using Windows. Is there a python shell that has a history of typed in
    >> commands?

    >
    > Is there a shell that doesn't have history then? At least both the
    > vanilla shell and Idle both have basic history in Windows. IPython for
    > more fun.


    The default interactive interpreter for Python doesn't have persistent
    history, so if you exit the interpreter and restart it, your commands are
    gone.


    --
    Steven
    Steven D'Aprano, Nov 23, 2011
    #8
  9. goldtech

    Tim Golden Guest

    On 23/11/2011 10:29, Steven D'Aprano wrote:
    > On Wed, 23 Nov 2011 11:23:19 +0200, Anssi Saari wrote:
    >
    >> goldtech<> writes:
    >>
    >>> Using Windows. Is there a python shell that has a history of typed in
    >>> commands?

    >>
    >> Is there a shell that doesn't have history then? At least both the
    >> vanilla shell and Idle both have basic history in Windows. IPython for
    >> more fun.

    >
    > The default interactive interpreter for Python doesn't have persistent
    > history, so if you exit the interpreter and restart it, your commands are
    > gone.


    Not quite

    The interpreter inherits the command shell's history function:
    Open a cmd window and then a Python session. Do some stuff.

    Ctrl-Z to exit to the surrounding cmd window.
    Do some random cmd stuff: dir, cd, etc.

    Start a second Python session. up-arrow etc. will bring back
    the previous Python session's commands (and not the ones you
    entered in the surrounding shell)

    Obviously this only applies when an underlying cmd session
    persists -- if you simply start Python from Start > Run
    twice the command history will not persist between sessions.

    TJG
    Tim Golden, Nov 23, 2011
    #9
  10. On Wed, 23 Nov 2011 10:37:56 +0000, Tim Golden wrote:

    > The interpreter inherits the command shell's history function: Open a
    > cmd window and then a Python session. Do some stuff.
    >
    > Ctrl-Z to exit to the surrounding cmd window. Do some random cmd stuff:
    > dir, cd, etc.
    >
    > Start a second Python session. up-arrow etc. will bring back the
    > previous Python session's commands (and not the ones you entered in the
    > surrounding shell)


    Doesn't work for me, at least not with Python 2.5 and 2.6 on Linux.

    I don't suppose you are running a site-specific command history script in
    your startup.py file?


    [steve@wow-wow ~]$ unset PYTHONSTARTUP
    [steve@wow-wow ~]$ python
    Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
    [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print 42

    42
    >>>

    [1]+ Stopped python
    [steve@wow-wow ~]$ python
    Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
    [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>



    You can't see it, but I'm hitting the up arrow on that last line, and
    nothing is happening except my console is flashing :)


    --
    Steven
    Steven D'Aprano, Nov 24, 2011
    #10
  11. On Thu, Nov 24, 2011 at 1:16 PM, Steven D'Aprano
    <> wrote:
    > On Wed, 23 Nov 2011 10:37:56 +0000, Tim Golden wrote:
    >
    >> The interpreter inherits the command shell's history function: Open a
    >> cmd window and then a Python session. Do some stuff.
    >>
    >> Ctrl-Z to exit to the surrounding cmd window. Do some random cmd stuff:
    >> dir, cd, etc.
    >>>>

    > [1]+  Stopped                 python


    Ctrl-Z is the Windows equivalent (well, mostly) of Linux's Ctrl-D. You
    want to cleanly exit the interpreter, not SIGSTOP it.

    ChrisA
    Chris Angelico, Nov 24, 2011
    #11
  12. On Thu, 24 Nov 2011 15:30:57 +1100, Chris Angelico wrote:

    > On Thu, Nov 24, 2011 at 1:16 PM, Steven D'Aprano
    > <> wrote:
    >> On Wed, 23 Nov 2011 10:37:56 +0000, Tim Golden wrote:
    >>
    >>> The interpreter inherits the command shell's history function: Open a
    >>> cmd window and then a Python session. Do some stuff.
    >>>
    >>> Ctrl-Z to exit to the surrounding cmd window. Do some random cmd
    >>> stuff: dir, cd, etc.
    >>>>>

    >> [1]+  Stopped                 python

    >
    > Ctrl-Z is the Windows equivalent (well, mostly) of Linux's Ctrl-D. You
    > want to cleanly exit the interpreter, not SIGSTOP it.



    One of us is confused, and I'm pretty sure it's you :)

    Tim went on to say "Obviously this only applies when an underlying cmd
    session persists", which I understood as implying that he too is using
    Linux where Ctrl-Z stops the process, but does not exit it.



    --
    Steven
    Steven D'Aprano, Nov 24, 2011
    #12
  13. On Thu, Nov 24, 2011 at 5:11 PM, Steven D'Aprano
    <> wrote:
    > One of us is confused, and I'm pretty sure it's you :)
    >
    > Tim went on to say "Obviously this only applies when an underlying cmd
    > session persists", which I understood as implying that he too is using
    > Linux where Ctrl-Z stops the process, but does not exit it.


    Entirely possible :) I blithely assumed from the fact that he said
    "dir" that it was Windows, but it goes to show what happens when you
    assume.

    ChrisA
    Chris Angelico, Nov 24, 2011
    #13
  14. goldtech

    Tim Golden Guest

    On 24/11/2011 06:22, Chris Angelico wrote:
    > On Thu, Nov 24, 2011 at 5:11 PM, Steven D'Aprano
    > <> wrote:
    >> One of us is confused, and I'm pretty sure it's you :)
    >>
    >> Tim went on to say "Obviously this only applies when an underlying cmd
    >> session persists", which I understood as implying that he too is using
    >> Linux where Ctrl-Z stops the process, but does not exit it.

    >
    > Entirely possible :) I blithely assumed from the fact that he said
    > "dir" that it was Windows, but it goes to show what happens when you
    > assume.


    Ahem. Sorry for any confusion caused. The OP was asking about the
    situation on Windows, and I was responding in that context. The
    Ctrl-Z thing is what *exits* the interpreter on Windows (a la Ctrl-D
    on Linux).

    In short - on Windows, within one cmd shell you can open and exit
    the interpreter as many times as you like and the Python command
    history will be retained via the cmd shell's history mechanism,
    and kept distinct from the history of other things you may type
    into the cmd shell.

    If you exit the cmd shell then that history is lost, and I'm not
    aware of any mechanism for retaining it.

    All this may or may not be of any use to the OP. I was responding
    to this comment by Steven:

    "The default interactive interpreter for Python doesn't have persistent
    history, so if you exit the interpreter and restart it, your commands
    are gone."


    TJG
    Tim Golden, Nov 24, 2011
    #14
  15. Am 17.11.2011 00:59, schrieb Ben Finney:
    > David Robinow<> writes:
    >> but your code works fine on Windows. Thanks.

    >
    > I'm glad to know that. Perhaps you could investigate why, and suggest an
    > update to the above documentation if it's wrong? The bug tracker at
    > <URL:http://bugs.python.org/> would be the appropriate place for such a
    > suggestion.


    Interestingly, on MS Windows (XP here), every commandline program
    inherits the history functionality (browsing with cursor up/down) from
    the shell it runs in. That means the program itself doesn't have to
    supply any of that, but also that it can't customize any of that...

    The history is not persistent though, it is restricted to that shell.
    Still, this might explain why it never bothered anyone enough to fix
    things properly. ;)

    Uli
    Ulrich Eckhardt, Nov 24, 2011
    #15
  16. On Thu, 24 Nov 2011 08:51:35 +0000, Tim Golden <>
    declaimed the following in gmane.comp.python.general:


    > In short - on Windows, within one cmd shell you can open and exit
    > the interpreter as many times as you like and the Python command
    > history will be retained via the cmd shell's history mechanism,
    > and kept distinct from the history of other things you may type
    > into the cmd shell.
    >

    Ah, so THAT is what the "number of buffers" control in "Command
    Prompt Properties/Options" is meant for...
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Nov 24, 2011
    #16
  17. goldtech

    alex23 Guest

    Tim Golden <> wrote:
    > The interpreter inherits the command shell's history function:
    > Open a cmd window and then a Python session. Do some stuff.
    >
    > Ctrl-Z to exit to the surrounding cmd window.
    > Do some random cmd stuff: dir, cd, etc.
    >
    > Start a second Python session. up-arrow etc. will bring back
    > the previous Python session's commands (and not the ones you
    > entered in the surrounding shell)


    This isn't true, at least not for ActivePython 2.7.2.5 under Windows
    7-64. The second session has no history whatsoever.
    alex23, Nov 25, 2011
    #17
  18. goldtech

    alex23 Guest

    On Nov 24, 6:51 pm, Tim Golden <> wrote:
    > The
    > Ctrl-Z thing is what *exits* the interpreter on Windows (a la Ctrl-D
    > on Linux).


    With ActivePython, Ctrl-D works as well, which is a godsend as I'm
    constantly working across Windows & linux.

    > In short - on Windows, within one cmd shell you can open and exit
    > the interpreter as many times as you like and the Python command
    > history will be retained via the cmd shell's history mechanism,
    > and kept distinct from the history of other things you may type
    > into the cmd shell.


    And again, I'm definitely not seeing this. Inside the one cmd shell,
    each instance of Python has no recollection of the history of the last.
    alex23, Nov 25, 2011
    #18
  19. goldtech

    Tim Golden Guest

    On 25/11/2011 03:47, alex23 wrote:
    > Tim Golden<> wrote:
    >> The interpreter inherits the command shell's history function:
    >> Open a cmd window and then a Python session. Do some stuff.
    >>
    >> Ctrl-Z to exit to the surrounding cmd window.
    >> Do some random cmd stuff: dir, cd, etc.
    >>
    >> Start a second Python session. up-arrow etc. will bring back
    >> the previous Python session's commands (and not the ones you
    >> entered in the surrounding shell)

    >
    > This isn't true, at least not for ActivePython 2.7.2.5 under Windows
    > 7-64. The second session has no history whatsoever.


    Well I don't know what to say. It works for me with an almost
    identical setup. (ActivePython 2.7.1.4 Win7 x64). And has worked
    for me over countless setups on different machines / versions of
    Windows / versions of Python etc.

    Do you have the pyreadline module installed? ISTR that that takes
    over from the standard cmd processing...

    TJG
    Tim Golden, Nov 25, 2011
    #19
  20. Am 25.11.2011 04:49, schrieb alex23:
    > On Nov 24, 6:51 pm, Tim Golden<> wrote:
    >> The Ctrl-Z thing is what *exits* the interpreter on Windows
    >> (a la Ctrl-D on Linux).

    >
    > With ActivePython, Ctrl-D works as well, which is a godsend as I'm
    > constantly working across Windows& linux.
    >
    >> In short - on Windows, within one cmd shell you can open and exit
    >> the interpreter as many times as you like and the Python command
    >> history will be retained via the cmd shell's history mechanism,
    >> and kept distinct from the history of other things you may type
    >> into the cmd shell.

    >
    > And again, I'm definitely not seeing this. Inside the one cmd shell,
    > each instance of Python has no recollection of the history of the
    > last.


    I'm seeing history browsing in Python on MS Windows XP here and it also
    works for every other commandline-based program. Well, it seems with the
    exception of the ActivePython distribution of Python. That one
    intentionally changes the MS Windows defaults like Control-Z behaviour
    and at the same time, maybe even as a side effect, it breaks the shell's
    history browsing.

    You don't happen to have an installation of the vanilla Python
    distribution to test, do you? This is getting me curious...

    Uli
    Ulrich Eckhardt, Nov 25, 2011
    #20
    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. Ersin Gençtürk
    Replies:
    1
    Views:
    3,463
    Ersin Gençtürk
    Oct 6, 2004
  2. Yingjie Lan
    Replies:
    4
    Views:
    286
    John Nagle
    Jan 29, 2010
  3. Sam Stephenson
    Replies:
    1
    Views:
    213
    Andrew Walrond
    Jun 18, 2005
  4. bwv549
    Replies:
    9
    Views:
    123
    Matthias Ludwig
    Aug 11, 2006
  5. Mista_Blista

    Counting device reboots

    Mista_Blista, Oct 13, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    105
    John W. Krahn
    Oct 13, 2003
Loading...

Share This Page