python shell that saves history of typed in commands that willpersist between reboots

G

goldtech

Hi,

Using Windows. Is there a python shell that has a history of typed in
commands?

I don't need output of commands just what I typed it. I need it to
save between sessions - something that no shell seems to do. If I
reboot there will still be a command history somewhere.

Like bash history in Linux.

Thanks
 
D

David Robinow

goldtech said:
Using Windows. Is there a python shell that has a history of typed in
commands?

I don't know about MS Windows, but the Python interactive shell can be
linked with the GNU Readline library for managing its command line
<URL:http://docs.python.org/library/readline.html> including editing
features, tab completion, history management, and a persistent history
file.

You can then use that functionality in your Python interactive startup
file. Here's mine:

=====
# $HOME/.pythonrc
# User configuration for interactive Python shell.

import sys
import os
import os.path
import atexit

# Tab completion with readline.
# Cribbed from <URL:http://docs.python.org/lib/module-rlcompleter.html>.
try:
   import readline
except ImportError:
   sys.stderr.write("Module readline not available.\n")
else:
   import rlcompleter

   # Enable tab completion.
   readline.parse_and_bind("tab: complete")

   # Persistent command history.
   histfile = os.path.join(os.environ["HOME"], ".python_history")
   try:
       readline.read_history_file(histfile)
   except IOError:
       # Existing history file can't be read.
       pass
   atexit.register(readline.write_history_file, histfile)

   del histfile

del sys, os, atexit

=====

Reading the documentation, I see that the ‘readline’ library is only
linked with Python on Unix-alike operating systems. Yet another reason
why MS Windows is not a good choice for developing software I guess.

I'm not sure what documentation you're reading, but your code works fine on
Windows. Thanks. [It is necessary to properly set PYTHONSTARTUP]
 
D

David Robinow

David Robinow said:
I don't know about MS Windows, but the Python interactive shell can be
linked with the GNU Readline library for managing its command line
<URL:http://docs.python.org/library/readline.html>
[…]
Reading the documentation, I see that the ‘readline’ library is only
linked with Python on Unix-alike operating systems.
 I'm not sure what documentation you're reading

The same documentation I linked to above. Immediately below the title,
it specifies a limited set of platforms: “Platforms: Unix” limiting the
availability of the described module.
but your code works fine on Windows. Thanks.

I'm glad to know that. Perhaps you could investigate why, and suggest an
update to the above documentation if it's wrong? The bug tracker at
<URL:http://bugs.python.org/> would be the appropriate place for such a
suggestion.

Upon further investigation, it turns out that I'm using pyreadline
from http://pypi.python.org/pypi/pyreadline. I'd forgotten I'd
installed it. No documentation fixes appear to be necessary.

"The pyreadline package is a python implementation of GNU readline
functionality it is based on the ctypes based UNC readline package by
Gary Bishop. It is not complete. It has been tested for use with
windows 2000 and windows xp."
It appears to work in Vista also, at least for the purposes
discussed in this thread.
 
A

alex23

You can then use that functionality in your Python interactive startup
file. Here's mine:

Awesome, thank you for this. I use iPython where ever possible but
there are times where I just can't avoid the default shell and this
will help immensely.

Cheers!
 
A

Anssi Saari

goldtech said:
Using Windows. Is there a python shell that has a history of typed in
commands?

Is there a shell that doesn't have history then? At least both the
vanilla shell and Idle both have basic history in Windows. IPython for
more fun.
 
S

Steven D'Aprano

Is there a shell that doesn't have history then? At least both the
vanilla shell and Idle both have basic history in Windows. IPython for
more fun.

The default interactive interpreter for Python doesn't have persistent
history, so if you exit the interpreter and restart it, your commands are
gone.
 
T

Tim Golden

The default interactive interpreter for Python doesn't have persistent
history, so if you exit the interpreter and restart it, your commands are
gone.

Not quite

The interpreter inherits the command shell's history function:
Open a cmd window and then a Python session. Do some stuff.

Ctrl-Z to exit to the surrounding cmd window.
Do some random cmd stuff: dir, cd, etc.

Start a second Python session. up-arrow etc. will bring back
the previous Python session's commands (and not the ones you
entered in the surrounding shell)

Obviously this only applies when an underlying cmd session
persists -- if you simply start Python from Start > Run
twice the command history will not persist between sessions.

TJG
 
S

Steven D'Aprano

The interpreter inherits the command shell's history function: Open a
cmd window and then a Python session. Do some stuff.

Ctrl-Z to exit to the surrounding cmd window. Do some random cmd stuff:
dir, cd, etc.

Start a second Python session. up-arrow etc. will bring back the
previous Python session's commands (and not the ones you entered in the
surrounding shell)

Doesn't work for me, at least not with Python 2.5 and 2.6 on Linux.

I don't suppose you are running a site-specific command history script in
your startup.py file?


[steve@wow-wow ~]$ unset PYTHONSTARTUP
[steve@wow-wow ~]$ python
Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.[1]+ Stopped python
[steve@wow-wow ~]$ python
Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

You can't see it, but I'm hitting the up arrow on that last line, and
nothing is happening except my console is flashing :)
 
C

Chris Angelico

The interpreter inherits the command shell's history function: Open a
cmd window and then a Python session. Do some stuff.

Ctrl-Z to exit to the surrounding cmd window. Do some random cmd stuff:
dir, cd, etc.
[1]+  Stopped                 python

Ctrl-Z is the Windows equivalent (well, mostly) of Linux's Ctrl-D. You
want to cleanly exit the interpreter, not SIGSTOP it.

ChrisA
 
S

Steven D'Aprano

The interpreter inherits the command shell's history function: Open a
cmd window and then a Python session. Do some stuff.

Ctrl-Z to exit to the surrounding cmd window. Do some random cmd
stuff: dir, cd, etc.
[1]+  Stopped                 python

Ctrl-Z is the Windows equivalent (well, mostly) of Linux's Ctrl-D. You
want to cleanly exit the interpreter, not SIGSTOP it.


One of us is confused, and I'm pretty sure it's you :)

Tim went on to say "Obviously this only applies when an underlying cmd
session persists", which I understood as implying that he too is using
Linux where Ctrl-Z stops the process, but does not exit it.
 
C

Chris Angelico

One of us is confused, and I'm pretty sure it's you :)

Tim went on to say "Obviously this only applies when an underlying cmd
session persists", which I understood as implying that he too is using
Linux where Ctrl-Z stops the process, but does not exit it.

Entirely possible :) I blithely assumed from the fact that he said
"dir" that it was Windows, but it goes to show what happens when you
assume.

ChrisA
 
T

Tim Golden

Entirely possible :) I blithely assumed from the fact that he said
"dir" that it was Windows, but it goes to show what happens when you
assume.

Ahem. Sorry for any confusion caused. The OP was asking about the
situation on Windows, and I was responding in that context. The
Ctrl-Z thing is what *exits* the interpreter on Windows (a la Ctrl-D
on Linux).

In short - on Windows, within one cmd shell you can open and exit
the interpreter as many times as you like and the Python command
history will be retained via the cmd shell's history mechanism,
and kept distinct from the history of other things you may type
into the cmd shell.

If you exit the cmd shell then that history is lost, and I'm not
aware of any mechanism for retaining it.

All this may or may not be of any use to the OP. I was responding
to this comment by Steven:

"The default interactive interpreter for Python doesn't have persistent
history, so if you exit the interpreter and restart it, your commands
are gone."


TJG
 
U

Ulrich Eckhardt

Am 17.11.2011 00:59, schrieb Ben Finney:
I'm glad to know that. Perhaps you could investigate why, and suggest an
update to the above documentation if it's wrong? The bug tracker at
<URL:http://bugs.python.org/> would be the appropriate place for such a
suggestion.

Interestingly, on MS Windows (XP here), every commandline program
inherits the history functionality (browsing with cursor up/down) from
the shell it runs in. That means the program itself doesn't have to
supply any of that, but also that it can't customize any of that...

The history is not persistent though, it is restricted to that shell.
Still, this might explain why it never bothered anyone enough to fix
things properly. ;)

Uli
 
D

Dennis Lee Bieber

In short - on Windows, within one cmd shell you can open and exit
the interpreter as many times as you like and the Python command
history will be retained via the cmd shell's history mechanism,
and kept distinct from the history of other things you may type
into the cmd shell.
Ah, so THAT is what the "number of buffers" control in "Command
Prompt Properties/Options" is meant for...
 
A

alex23

Tim Golden said:
The interpreter inherits the command shell's history function:
Open a cmd window and then a Python session. Do some stuff.

Ctrl-Z to exit to the surrounding cmd window.
Do some random cmd stuff: dir, cd, etc.

Start a second Python session. up-arrow etc. will bring back
the previous Python session's commands (and not the ones you
entered in the surrounding shell)

This isn't true, at least not for ActivePython 2.7.2.5 under Windows
7-64. The second session has no history whatsoever.
 
A

alex23

The
Ctrl-Z thing is what *exits* the interpreter on Windows (a la Ctrl-D
on Linux).

With ActivePython, Ctrl-D works as well, which is a godsend as I'm
constantly working across Windows & linux.
In short - on Windows, within one cmd shell you can open and exit
the interpreter as many times as you like and the Python command
history will be retained via the cmd shell's history mechanism,
and kept distinct from the history of other things you may type
into the cmd shell.

And again, I'm definitely not seeing this. Inside the one cmd shell,
each instance of Python has no recollection of the history of the last.
 
T

Tim Golden

This isn't true, at least not for ActivePython 2.7.2.5 under Windows
7-64. The second session has no history whatsoever.

Well I don't know what to say. It works for me with an almost
identical setup. (ActivePython 2.7.1.4 Win7 x64). And has worked
for me over countless setups on different machines / versions of
Windows / versions of Python etc.

Do you have the pyreadline module installed? ISTR that that takes
over from the standard cmd processing...

TJG
 
U

Ulrich Eckhardt

Am 25.11.2011 04:49, schrieb alex23:
With ActivePython, Ctrl-D works as well, which is a godsend as I'm
constantly working across Windows& linux.


And again, I'm definitely not seeing this. Inside the one cmd shell,
each instance of Python has no recollection of the history of the
last.

I'm seeing history browsing in Python on MS Windows XP here and it also
works for every other commandline-based program. Well, it seems with the
exception of the ActivePython distribution of Python. That one
intentionally changes the MS Windows defaults like Control-Z behaviour
and at the same time, maybe even as a side effect, it breaks the shell's
history browsing.

You don't happen to have an installation of the vanilla Python
distribution to test, do you? This is getting me curious...

Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top