preserve history in the interactive python

Discussion in 'Python' started by Nikhil, May 20, 2008.

  1. Nikhil

    Nikhil Guest

    Hi,

    I am using python -i, and I find it hard typing/pasting the commands
    from the previous interactive shell. Basically, is there anyway that I
    can preserve the history in the shell?
    I guess but not sure there should be something like ~/.pyrc for
    configuring such but can someone please let me know what is the
    effective environment variable to preserve the history?

    Thanks,
    Nikhil
     
    Nikhil, May 20, 2008
    #1
    1. Advertising

  2. Nikhil

    Nikhil Guest

    Nikhil wrote:
    > Hi,
    >
    > I am using python -i, and I find it hard typing/pasting the commands
    > from the previous interactive shell. Basically, is there anyway that I
    > can preserve the history in the shell?
    > I guess but not sure there should be something like ~/.pyrc for
    > configuring such but can someone please let me know what is the
    > effective environment variable to preserve the history?
    >
    > Thanks,
    > Nikhil
    >>there should be something like ~/.pyrc

    please read it as something in a file pointed by PYTHONSTARTUP
    environment variable. I am mostly here interested in saving the python
    shell's history automatically.

    Nikhil
     
    Nikhil, May 20, 2008
    #2
    1. Advertising

  3. Nikhil

    Nikhil Guest

    Nikhil wrote:
    > Hi,
    >
    > I am using python -i, and I find it hard typing/pasting the commands
    > from the previous interactive shell. Basically, is there anyway that I
    > can preserve the history in the shell?
    > I guess but not sure there should be something like ~/.pyrc for
    > configuring such but can someone please let me know what is the
    > effective environment variable to preserve the history?
    >
    > Thanks,
    > Nikhil

    I figured it out. This below thing works fine for me.
    BTW, I got it from http://docs.python.org/tut/node15.html. A little
    search would not hurt ;-)


    $ echo $PYTHONSTARTUP
    /u/me/.pyrc
    $ cat .pyrc
    import sys
    import atexit
    import os
    import readline
    import rlcompleter

    myprompt='$ '
    myhistoryfile="/u/me/.pyhistory"

    #set the prompt
    sys.ps1=myprompt

    #save the history
    historyPath = os.path.expanduser(myhistoryfile)

    def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

    if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

    atexit.register(save_history)
    del os, atexit, readline, rlcompleter, save_history, historyPath
     
    Nikhil, May 20, 2008
    #3
  4. Nikhil <> writes:

    > Nikhil wrote:
    >> Hi,
    >>
    >> I am using python -i, and I find it hard typing/pasting the commands
    >> from the previous interactive shell. Basically, is there anyway that
    >> I can preserve the history in the shell?
    >> I guess but not sure there should be something like ~/.pyrc for
    >> configuring such but can someone please let me know what is the
    >> effective environment variable to preserve the history?
    >>
    >> Thanks,
    >> Nikhil

    > I figured it out. This below thing works fine for me.
    > BTW, I got it from http://docs.python.org/tut/node15.html. A little
    > search would not hurt ;-)


    Or you could use IPython (http://ipython.scipy.org/), "an Enhanced
    Python Shell".

    --
    Arnaud
     
    Arnaud Delobelle, May 20, 2008
    #4
  5. Hi,

    On Tue, May 20, 2008 at 8:19 PM, Nikhil <> wrote:
    > Nikhil wrote:
    >> the previous interactive shell. Basically, is there anyway that I can
    >> preserve the history in the shell?

    >
    > I figured it out. This below thing works fine for me.
    > BTW, I got it from http://docs.python.org/tut/node15.html. A little search


    Didn't even think of that, now that I have it: I love you - it's great.

    To the one that implemented this: If you ever come to vienna, drop me
    a note I'll get you a $FAVORITE_DRINK_HERE

    /martin

    --
    http://www.xing.com/profile/Martin_Marcher

    You are not free to read this message,
    by doing so, you have violated my licence
    and are required to urinate publicly. Thank you.
     
    Martin Marcher, May 20, 2008
    #5
    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. Christoph Becker-Freyseng

    dumping command-history in python interactive mode

    Christoph Becker-Freyseng, Jul 17, 2003, in forum: Python
    Replies:
    3
    Views:
    405
    Pedro Werneck
    Jul 18, 2003
  2. POYEN OP Olivier (DCL)

    RE: dumping command-history in python interactive mode

    POYEN OP Olivier (DCL), Jul 18, 2003, in forum: Python
    Replies:
    0
    Views:
    401
    POYEN OP Olivier (DCL)
    Jul 18, 2003
  3. Ashot
    Replies:
    22
    Views:
    4,236
    Fernando Perez
    Feb 8, 2005
  4. ThomasC
    Replies:
    6
    Views:
    368
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    239
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page