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?

- 33,530
- 16
- 61
- 61
20 Answers
IPython is extremely useful if you like using interactive sessions. For example for your use-case there is the %save
magic 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).
Furthermore, the documentation states:
This function uses the same syntax as %history for input ranges, then saves the lines to the filename you specify.
This allows for example, to reference older sessions, such as
%save current_session ~0/
%save previous_session ~1/
Look at the videos on the presentation page to get a quick overview of the features.

- 10,307
- 6
- 65
- 88

- 53,288
- 15
- 90
- 97
-
13How to save all the lines? Without specifying the range, it creates an empty file. :( – balki Sep 01 '11 at 19:37
-
45@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
-
2Also, is it possible to load this file back into ipython and keep your input history intact? – Ben Page Sep 22 '11 at 10:58
-
hmm, I get SyntaxError: invalid syntax when I try it in the Enthought pylab prompt ... it's supposed to be IPython 12.1, and I can't find this save command documented on the IPython site ... – Sam Joseph Jul 17 '12 at 13:55
-
@SamJoseph: Please use %save my_useful_session 10-20 23 (as in the example post) don't forget the percentage-sign – feinmann Aug 23 '12 at 12:36
-
feinmann - thanks - that's it - maybe I can edit the answer to make that obvious ... but apparently not as edits have to be at least 6 characters ... – Sam Joseph Aug 31 '12 at 12:40
-
18@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
-
1Where is this function? When I try from ipython, there is a save function but "help(save)" says save is not defined. If I load ipython --pylab, then the "save" function is not the same, it is the one from numpy. I want to call ipython.save from ipython but when I'm using pylab if possible. – mathtick Jan 08 '13 at 21:54
-
@mathtick: there's a difference between Python functions and IPython commands (latter are like a shell built-in in *nix) - so command to use is %save which is part if IPython. See http://ipython.org/ipython-doc/rel-0.10.2/html/interactive/tutorial.html#source-code-handling-tips – RichVel May 04 '13 at 17:37
-
1If you want to append to the file rather than overwrite it, save using the `-a` option. Example: `%save -a my_useful_session 10-20 23` – Ryan Endacott Nov 24 '14 at 19:57
-
2So how does one restore the %save afterwards? – user4779 Dec 18 '15 at 13:13
-
10@user4779, as one would expect: `%load my_useful_session` – Daniel Serodio Jan 05 '17 at 02:23
-
The simplest way by far is Linux Python2 is Roberto's answer below. All built-in and just plain works. `import readline` then `readline.write_history_file('/home/ahj/history')` – SDsolar Nov 20 '17 at 00:43
-
Problem is I'm trying to use virtualenv which doesn't play nice with jupyter – Script Kitty Aug 24 '18 at 21:44
-
1@balki the whole current session can be specified with `~0/` – CharlesB Nov 30 '20 at 13:22
-
seems that there is not proper way of doing this in python if everyone is trying to ram jupyter down our throats. which I'm still really really iffy about. – John Sohn Sep 30 '21 at 13:42
-
how to save just few lines from interactive session in pycharm – Saran Zeb May 19 '22 at 17:51
From Andrew Jones's website (archived):
import readline
readline.write_history_file('/home/ahj/history')

- 32,384
- 7
- 42
- 56

- 2,107
- 1
- 12
- 2
-
11
-
14
-
5@ubershmekel - Looks like it [only works on Unix](https://docs.python.org/2.6/library/readline.html) – Joel B Feb 05 '15 at 15:04
-
39
-
4Remember to do it at the end of your session. Mine was looking a bit empty after calling the function only at the start. – dolphus333 Apr 27 '16 at 22:50
-
works on Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win 32; Windows 7 – Jellema Mar 08 '17 at 15:38
-
1
-
-
1PERFECT for Python2 on Ubuntu 16.04 LTS and on the Raspberry Pi. No special sauce required. All built-in. Thanks much! – SDsolar Nov 20 '17 at 00:41
-
1
-
1Works on Ubuntu 16.04.3 Python 3.5.2, but it saves all history instead of just the current session – Yushin Washio May 08 '18 at 10:35
-
Link seems to be broken now, just shows `connecting...` white screen – lacostenycoder Mar 09 '19 at 10:31
-
1
-
1This gave me the whole history of Python sessions on my computer, months of programming. Was interesting. – foxesque Mar 12 '22 at 12:52
-
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
and then set the environment variable PYTHONSTARTUP
in your shell (e.g. in ~/.bashrc
):
export PYTHONSTARTUP=$HOME/.pystartup
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.

- 26,083
- 8
- 103
- 158

- 111,714
- 37
- 173
- 152
-
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 03 '09 at 23:45
-
That was crazy fast, Nadia, many thanks. I will try both of the answers -- target platform is Ubuntu, BTW – unmounted Jun 04 '09 at 00:01
-
`readline.parse_and_bind('tab: complete')` works if you use MacPorts Python. – Phillip Cloud Jun 05 '12 at 19:04
-
3
-
Why I didn't search for this all of these years! This solution is great as uses original tool that is fast and light. – iman Jun 17 '17 at 18:39
-
just a "me too" to say that I use this manually when debugging unstable code since the atexit doesn't happen if the interpreter segfaults. Thanks again for the great suggestion! – taranaki Nov 09 '17 at 20:59
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:
%history -f /tmp/history.py

- 1,051
- 9
- 10
-
Wasnt sure about the location of the file using `save` magic. This was very helpful – alpha_989 Feb 02 '18 at 18:30
-
6Use `%history -g -f full_history.py` to get history from all sessions, not just the current one. See also: [documentation for %history](http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-history). – Evgeni Sergeev Mar 28 '18 at 00:38
After installing Ipython, and opening an Ipython session by running the command:
ipython
from your command line, just run the following Ipython 'magic' command to automatically log your entire Ipython session:
%logstart
This will create a uniquely named .py file and store your session for later use as an interactive Ipython session or for use in the script(s) of your choosing.

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

- 364,293
- 75
- 561
- 662
-
4`reinteract` is now superseded by the IPython Notebook and QtConsole. – Eric O. Lebigot Jan 26 '14 at 17:17
I had to struggle to find an answer, I was very new to iPython environment.
This will work
If your iPython session looks like this
In [1] : import numpy as np
....
In [135]: counter=collections.Counter(mapusercluster[3])
In [136]: counter
Out[136]: Counter({2: 700, 0: 351, 1: 233})
You want to save lines from 1 till 135 then on the same ipython session use this command
In [137]: %save test.py 1-135
This will save all your python statements in test.py file in your current directory ( where you initiated the ipython).

- 2,382
- 26
- 15
In addition to IPython, a similar utility bpython has a "save the code you've entered to a file" feature
-
3can anybody explain how this is done with bpython? I tried ctrl+s but it didn't work (version 0.14.2 on top of Python 3.4.3 running Gnome terminal) – Yibo Yang Jan 25 '16 at 07:38
-
It's bound to F7 by default. F1 will show you the help and current keybindings. – Jim K. Jun 06 '17 at 20:30
-
F7 launches the external editor. Ctrl+s should prompt for a simple save to file feature, but it's bound to "stop" in bash and zsh, so you'll need to choose an alternative in the bpython config. – RCross Feb 21 '20 at 11:22
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
:
>>> %hist -f 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:
%pastebin 3 18-20 ~1/1-5
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.
Stores variables, aliases and macros in IPython’s database.
d = {'a': 1, 'b': 2}
%store d # stores the variable
del d
%store -r d # Refresh the variable from IPython's database.
>>> d
{'a': 1, 'b': 2}
To autorestore stored variables on startup, specifyc.StoreMagic.autorestore = True
in ipython_config.py.

- 105,104
- 32
- 201
- 196
-
Enabling saving output history can be done by enabling `c.HistoryManager.db_log_output` in the config file. – Ikke Jun 03 '16 at 07:34
The %history
command is awesome, but unfortunately it won't let you save things that were %paste 'd into the sesh. To do that I think you have to do %logstart
at the beginning (although I haven't confirmed this works).
What I like to do is
%history -o -n -p -f filename.txt
which will save the output, line numbers, and '>>>' before each input (o, n, and p options). See the docs for %history here.

- 1
- 1

- 13,746
- 5
- 87
- 117
As far as Linux goes, one can use script
command to record the whole session. It is part of util-linux
package so should be on most Linux systems . You can create and alias or function that will call script -c python
and that will be saved to a typescript
file. For instance, here's a reprint of one such file.
$ cat typescript
Script started on Sat 14 May 2016 08:30:08 AM MDT
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello Pythonic World'
Hello Pythonic World
>>>
Script done on Sat 14 May 2016 08:30:42 AM MDT
Small disadvantage here is that the script
records everything , even line-feeds, whenever you hit backspaces , etc. So you may want to use col
to clean up the output (see this post on Unix&Linux Stackexchange) .

- 1
- 1

- 938
- 1
- 13
- 41
there is another option --- pyslice. in the "wxpython 2.8 docs demos and tools", there is a open source program named "pyslices".
you can use it like a editor, and it also support using like a console ---- executing each line like a interactive interpreter with immediate echo.
of course, all the blocks of codes and results of each block will be recorded into a txt file automatically.
the results are logged just behind the corresponding block of code. very convenient.

- 2,740
- 2
- 14
- 15
-
PySlices author here. Glad you like PySlices (and that you even found it). It's not quite abandon-ware yet (I actually still use it), but the version included in wxPython itself often doesn't work well. If you want to follow updates you can get them from the wx_py package: https://pypi.python.org/pypi/wx_py, https://github.com/davidmashburn/wx_py – David Sep 23 '16 at 16:40
In IPython, I first use
In [2]: %hist
to view my past code. I select the chunk I want to save and then paste it into file my_file.py
using the %%file
magic (short for %%writefile
)
In [3]: %%file my_file.py
...: # paste code here
...:
...:
hitting return two times in the end.
To append to file use the option -a
: %%file -a my_file.py
.
If needed, I can list, edit, etc. the file in the underlying command line using the exclamation mark
In [5]: !ls -l my_file.py
In [6]: !vi my_file.py

- 27,088
- 20
- 102
- 114
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 the len(IN)-1
shows the current input prompt in IPython, allowing you to save the whole session programmatically.

- 182
- 3
For those using spacemacs
, and ipython
that comes with python-layer
, save magic creates a lot of unwanted output, because of the constant auto-completion command working in the backround such as:
len(all_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all_substa'''))
len(all_substantives_w_suffixes)
';'.join(__PYTHON_EL_get_completions('''len'''))
';'.join(__PYTHON_EL_get_completions('''all'''))
';'.join(__PYTHON_EL_get_completions('''all_'''))
';'.join(__PYTHON_EL_get_completions('''all_w'''))
';'.join(__PYTHON_EL_get_completions('''all_wo'''))
';'.join(__PYTHON_EL_get_completions('''all_wor'''))
';'.join(__PYTHON_EL_get_completions('''all_word'''))
';'.join(__PYTHON_EL_get_completions('''all_words'''))
len(all_words_w_logograms)
len(all_verbs)
To avoid this just save the ipython buffer like you normally save any other: spc f s

- 515
- 4
- 16
If you use bpython, all your command history is by default saved to ~/.pythonhist
.
To save the commands for later reusage you can copy them to a python script file:
$ cp ~/.pythonhist mycommands.py
Then edit that file to clean it up and put it under Python path (global or virtual environment's site-packages, current directory, mentioning in *.pth, or some other way).
To include the commands into your shell, just import them from the saved file:
>>> from mycommands import *

- 3,965
- 1
- 30
- 45
I'd like to suggest another way to maintain python session through tmux on linux. you run tmux, attach your self to the session you opened (if not attached after opening it directly). execute python and do whatever you are doing on it. then detach from session. detaching from a tmux session does not close the session. the session remains open.
pros of this method: you can attach to this session from any other device (in case you can ssh your pc)
cons of this method: this method does not relinquish the resources used by the opened python session until you actually exist the python interpreter.

- 326
- 5
- 14
To save input and output on XUbuntu:
- In XWindows, run iPython from the Xfce terminal app
- click
Terminal
in the top menu bar and look forsave contents
in the dropdown
I find this saves the input and output, going all the way back to when I opened the terminal. This is not ipython specific, and would work with ssh sessions or other tasks run from the terminal window.

- 26,170
- 12
- 85
- 119
You can use built-in function open: I use it in all of my programs in which I need to store some history (Including Calculator, etc.) for example:
#gk-test.py or anything else would do
try: # use the try loop only if you haven't created the history file outside program
username = open("history.txt").readline().strip("\n")
user_age = open("history.txt").readlines()[1].strip("\n")
except FileNotFoundError:
username = input("Enter Username: ")
user_age = input("Enter User's Age: ")
open("history.txt", "w").write(f"{username}\n{user_age}")
#Rest of the code is secret! try it your own!
I would thank to all of them who liked my comments! Thank you for reading this!

- 11
- 1