preserve history in the interactive python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikhil

    preserve history in the interactive python

    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

    #2
    Re: preserve history in the interactive python

    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

    Comment

    • Nikhil

      #3
      Re: preserve history in the interactive python

      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=mypromp t

      #save the history
      historyPath = os.path.expandu ser(myhistoryfi le)

      def save_history(hi storyPath=histo ryPath):
      import readline
      readline.write_ history_file(hi storyPath)

      if os.path.exists( historyPath):
      readline.read_h istory_file(his toryPath)

      atexit.register (save_history)
      del os, atexit, readline, rlcompleter, save_history, historyPath

      Comment

      • Arnaud Delobelle

        #4
        Re: preserve history in the interactive python

        Nikhil <mnikhil@gmail. comwrites:
        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

        Comment

        Working...