How do I repeat the last command? The usual keys: Up, Ctrl+Up, Alt-p don\'t work. They produce nonsensical characters.
(ve)[kakarukeys@localhost ve]$ python
I use the following to enable history on python shell.
This is my .pythonstartup file . PYTHONSTARTUP environment variable is set to this file path.
# 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
You will need to have the modules readline, rlcompleter to enable this.
Check out the info on this at : http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP.
Modules required:
On Ubuntu Server 12.04, I had this problem after installing a version of Python from source (Python3.4).
Some of the comments here recommend installing Ipython and I want to mention that I have the same behavior even with Ipython. From what I can tell, this is a readline problem.
For Ubuntu 12.04 server, I had to install libncurses-dev
and libreadline-dev
and then install Python from source for up-history (readline) behavior to be enabled. I pretty much did this:
sudo apt-get install libncurses-dev libreadline-dev
After that, I deleted the previously installed Python (NOT THE SYSTEM PYTHON, the one I had installed from source!) and reinstalled it from source and everything worked as expected.
I did not have to install anything with pip or edit .pythonstartup.
On Ubuntu 16.04, I had the same problem after upgrading Python from the preloaded 3.5 to version 3.7 from source code. As @erewok suggested, I did
sudo apt-get install libncurses-dev libreadline-dev
followed by:
sudo make install
After that, the arrow-up key worked. Not sure which module is required to fix the problem or both, but without "make install", none would work. During initial make, there were some red-flag errors, but ignored and completed the build. This time, there didn't seem to have any errors.
In IDLE, go to Options -> Configure IDLE -> Keys and there select history-next and then history-previous to change the keys.
Then click on Get New Keys for Selection and you are ready to choose whatever key combination you want.
You don't need a custom script like pyfunc's answer for OSX (at least on mavericks). In Idle click on Idle -> Preferences -> Keys, locate "history-next" and "history-previous", and either leave them with their default keyboard shortcut or assign "up arrow" and "down arrow" per typical expected terminal behavior.
This is on Idle 2.7 on OSX Mavericks.
On Mac with Python 2.x
➜ ~ brew install rlwrap
Start with rlwrap
➜ ~ rlwrap python