Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

preserve history in the interactive python

0 views
Skip to first unread message

Nikhil

unread,
May 20, 2008, 2:12:13 PM5/20/08
to
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

unread,
May 20, 2008, 2:14:56 PM5/20/08
to
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

unread,
May 20, 2008, 2:19:17 PM5/20/08
to
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

Arnaud Delobelle

unread,
May 20, 2008, 2:38:48 PM5/20/08
to
Nikhil <mni...@gmail.com> 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

Martin Marcher

unread,
May 20, 2008, 5:44:36 PM5/20/08
to pytho...@python.org
Hi,

On Tue, May 20, 2008 at 8:19 PM, Nikhil <mni...@gmail.com> 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.

0 new messages