preserve history in the interactive python

N

Nikhil

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
 
N

Nikhil

Nikhil said:
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
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
 
N

Nikhil

Nikhil said:
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
 
M

Martin Marcher

Hi,

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top