Jay Taylor's notes

back to listing index

shell - How to save a Python interactive session? - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: stackoverflow.com
Clipped on: 2013-02-13

I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell (db connections, variable assignments, little for loops and bits of logic) -- some history of the interactive session? If I use something like script I get too much stdout noise. I don't really need to pickle all the objects -- though if there is a solution that does that, it would be OK. Ideally I would just be left with a script that ran as the one I created interactively, and I could just delete the bits I didn't need. Is there a package that does this, or a DIY approach?

UPDATE: I am really amazed at the quality and usefulness of these packages. For those with a similar itch:

  • IPython -- should have been using this for ages, kind of what I had in mind
  • reinteract -- very impressive, I want to learn more about visualization and this seems like it will shine there. Sort of a gtk/gnome desktop app that renders graphs inline. Imagine a hybrid shell + graphing calculator + mini eclipse. Source distribution here: http://www.reinteract.org/trac/wiki/GettingIt . Built fine on Ubuntu, integrates into gnome desktop, Windows and Mac installers too.
  • bpython -- extremely cool, lots of nice features, autocomplete(!), rewind, one keystroke save to file, indentation, well done. Python source distribution, pulled a couple of dependencies from sourceforge.

I am converted, these really fill a need between interpreter and editor.

edited Jan 2 at 0:09
Image (Asset 1/9) alt= 1
asked Jun 3 '09 at 23:21
Image (Asset 2/9) alt= 6,95132837
add comment

7 Answers

up vote 58 down vote accepted

IPython is extremely useful if you like using interactive sessions. For example for your usecase there is the save command, you just input save my_useful_session 10-20 23 to save input lines 10 to 20 and 23 to my_useful_session.py. (to help with this, every line is prefixed by its number)

Look at the videos on the documentation page to get a quick overview of the features.

answered Jun 3 '09 at 23:34
Image (Asset 3/9) alt= 17.4k13261
2 upvote
 flag
This is very good on Ubuntu and feels revolutionary on Windows, it was was a huge missing piece for me on that platform. – bvmou Jul 16 '09 at 22:49
  upvote
 flag
How to save all the lines? Without specifying the range, it creates an empty file. :( – balki Sep 1 '11 at 19:37
2 upvote
 flag
@balki, IPython's prompt tells you how many lines are in your history (i.e. In[48]). So save filename 1-48 would save your whole session. – Ben Page Sep 22 '11 at 10:51
1 upvote
 flag
Also, is it possible to load this file back into ipython and keep your input history intact? – Ben Page Sep 22 '11 at 10:58
1 upvote
 flag
@BenPage Use "ipython -i [filename]" on the saved .py file, from the bash promt in order to load the file back before returning to an interactive console! (without the -i flag you don't get the interactive console after running the file). – Samuel Lampa Nov 29 '12 at 14:30
add / show 4 more comments

There is a way to do it. Store the file in ~/.pystartup

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

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

You can also add this to get autocomplete for free:

readline.parse_and_bind('tab: complete')

Please note that this will only work on *nix systems. As readline is only available in Unix platform.

answered Jun 3 '09 at 23:23
Image (Asset 4/9) alt= 28.4k471104
  upvote
 flag
Mac OS X uses editline, so there is tab-complete functionality available, but the exact command is different: readline.parse_and_bind("bind ^I rl_complete") – Miles Jun 3 '09 at 23:45
  upvote
 flag
@Miles, thanks for the info – Nadia Alramli Jun 3 '09 at 23:48
  upvote
 flag
That was crazy fast, Nadia, many thanks. I will try both of the answers -- target platform is Ubuntu, BTW – bvmou Jun 4 '09 at 0:01
  upvote
 flag
readline.parse_and_bind('tab: complete') works if you use MacPorts Python. – cpcloud Jun 5 '12 at 19:04
  upvote
 flag
it worked like a charm in cygwin! thanks! – Pedro NF Oct 9 '12 at 11:28
add comment

Also, reinteract gives you a notebook-like interface to a Python session.

answered Jun 4 '09 at 0:28
Image (Asset 5/9) alt= 96.3k11139270
  upvote
 flag
That is extremely cool. – bvmou Jun 4 '09 at 7:54
add comment

In addition to IPython, a similar utility bpython has a "save the code you've entered to a file" feature

answered Jun 4 '09 at 1:03
Image (Asset 6/9) alt= 46.9k19119220
  upvote
 flag
This is great, I should be putting up screenshots. – bvmou Jun 4 '09 at 8:01
add comment

On windows, PythonWin is a lot more productive than that the default python terminal. It has a lot of features that you usually find in IDEs:

  • save the terminal session to a file
  • colored syntax highlighting
  • code completion for classes/properties/variables when you press tab.
  • properly browsing when you type "."
  • parameter hints when you type "("
  • it's a GUI rather than a DOS window, so you have easier copy/paste and autowrapping long lines if you resize the window.

You can download it as part of Python for Windows extensions http://sourceforge.net/projects/pywin32/files/pywin32/

answered Mar 6 '12 at 23:29
Image (Asset 7/9) alt= 797816
add comment

http://www.andrewhjon.es/save-interactive-python-session-history

import readline
readline.write_history_file('/home/ahj/history')
answered Mar 15 '12 at 13:06
Image (Asset 8/9) alt= 6111
add comment

Just putting another suggesting in the bowl: Spyderlib

answered May 6 '12 at 21:25
Image (Asset 9/9) alt= 703413
add comment

Your Answer

 
community wiki

Not the answer you're looking for? Browse other questions tagged or ask your own question.