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.
Some comments were asking how to save all of the IPython inputs at once. For %save magic in IPython, you can save all of the commands programmatically as shown below, to avoid the prompt message and also to avoid specifying the input numbers. currentLine = len(In)-1 %save -f my_session 1-$currentLine
The
-f
option is used for forcing file replacement and thelen(IN)-1
shows the current input prompt in IPython, allowing you to save the whole session programmatically.There is a way to do it. Store the file in
~/.pystartup
...and then set the environment variable
PYTHONSTARTUP
in your shell (e.g. in~/.bashrc
):You can also add this to get autocomplete for free:
Please note that this will only work on *nix systems. As readline is only available in Unix platform.
Also, reinteract gives you a notebook-like interface to a Python session.
If you are using IPython you can save to a file all your previous commands using the magic function %history with the -f parameter, p.e:
There is %history magic for printing and saving the input history (and optionally the output).
To store your current session to a file named
my_history.py
:History IPython stores both the commands you enter, and the results it produces. You can easily go through previous commands with the up- and down-arrow keys, or access your history in more sophisticated ways.
You can use the %history magic function to examine past input and output. Input history from previous sessions is saved in a database, and IPython can be configured to save output history.
Several other magic functions can use your input history, including %edit, %rerun, %recall, %macro, %save and %pastebin. You can use a standard format to refer to lines:
This will take line 3 and lines 18 to 20 from the current session, and lines 1-5 from the previous session.
See %history? for the Docstring and more examples.
Also, be sure to explore the capabilities of %store magic for lightweight persistence of variables in IPython.
To autorestore stored variables on startup, specify
c.StoreMagic.autorestore = True
in ipython_config.py.Just putting another suggesting in the bowl: Spyder
It has History log and Variable explorer. If you have worked with MatLab, then you'll see the similarities.