Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Python interpreter history to a file?

Tags:

Many times I will use the Python interpreter to inspect variables and step through commands before I actually write to a file. However by the end I have around 30 commands in the interpreter, and have to copy/paste them into a file to run. Is there a way I can export/write the Python interpreter history into a file?

For example

>>> a = 5 >>> b = a + 6 >>> import sys >>> export('history', 'interactions.py')  

And then I can open the interactions.py file and read:

a = 5 b = a + 6 import sys 
like image 991
Kevin Burke Avatar asked Dec 07 '11 19:12

Kevin Burke


People also ask

Where is Python history stored?

The History Log is stored in the . spyder-py3 (Python 3) or spyder (Python 2) directory in your user home folder (by default, C:/Users/username on Windows, /Users/username for macOS, and typically /home/username on GNU/Linux).

How do I save an interactive session in Python?

To save a Python interactive session, we can use the readline module. to call readline. write_history_file to save the interactive session content into the /home/ahj/history file.

How do I save a file in Python shell?

Right-click the Python window and select Save As to save your code either as a Python file (. py) or Text file (. txt). If saving to a Python file, only the Python code will be saved.


2 Answers

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.

::OR::

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.

like image 96
Mr. Elusive Avatar answered Nov 08 '22 07:11

Mr. Elusive


If you are using Linux/Mac and have readline library, you could add the following to a file and export it in your .bash_profile and you will have both completion and history.

# python startup file import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind('tab: complete') # history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try:     readline.read_history_file(histfile) except IOError:     pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter 

Export command:

export PYTHONSTARTUP=path/to/.pythonstartup 

This will save your python console history at ~/.pythonhistory

like image 28
pyfunc Avatar answered Nov 08 '22 06:11

pyfunc