1

Possible Duplicate:
How to save a Python interactive session?

Can i save everything I type into a python session when "brain storming"?

For instance, not just default variables but of course even overriding the shell. I of course mean by invoking the actual python executable.

I seriously hope this is not a stupid question.

I need rep of course too, so this probes me a bit.

Community
  • 1
  • 1
phreaki
  • 111
  • 1
  • 6

4 Answers4

9

iPython (as suggested in another answer) is indeed a good suggestion, but if you prefer the good old Python interactive interpreter it's not too hard to do it there either. Set your environment variable PYTHONSTARTUP to point to a file that contains, for example:

import atexit
import readline
try:
    readline.read_history_file('.PythonHistory')
except OSError:
    pass
atexit.register(lambda: readline.write_history_file('.PythonHistory'))

this can be tweaked as you wish (e.g. to load and save the same file no matter what directory you're starting from) but I kind of like this simple version as it makes it very easy to have different "sessions" remembered in different working directories.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
3

Not sure if you can do this with the Python shell. But it's possible with IPython which gives you a lot more:

ars
  • 120,335
  • 23
  • 147
  • 134
2

Others (ars, Alex Martelli) have given direct answers to the question. For myself, I've found a more effective strategy is to write all of the commands into a text editors and either execute saved scripts and/or copy-and-paste into python or ipython. I find that I can keep myself more organized that way.

Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
0

There is as well the Bpython interpreter :

http://www.bpython-interpreter.org/

This is the list of features which include the save code and even send the code to a pastebin service.

  • In-line syntax highlighting.
  • Readline-like autocomplete with suggestions displayed as you type.
  • Expected parameter list for any Python function.
  • "Rewind" function to pop the last line of code from memory and re-evaluate.
  • Send the code you've entered off to a pastebin.
  • Save the code you've entered to a file.
  • Auto-indentation.
Chmouel Boudjnah
  • 2,541
  • 3
  • 24
  • 28