How to repeat last command in python shell shell?

How can I repeat the last command? Normal keys: Up, Ctrl + Up, Alt-p do not work. They produce meaningless characters.

(ve)[kakarukeys@localhost ve]$ python Python 2.6.6 (r266:84292, Nov 15 2010, 21:48:32) [GCC 4.4.4 20100630 (Red Hat 4.4.4-10)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "hello world" hello world >>> ^[[A File "<stdin>", line 1 ^ SyntaxError: invalid syntax >>> ^[[1;5A File "<stdin>", line 1 [1;5A ^ SyntaxError: invalid syntax >>> ^[p File "<stdin>", line 1 p ^ SyntaxError: invalid syntax >>> 
+95
python shell virtualenv interpreter python-idle
Nov 27 2018-10-11T00:
source share
25 answers

I use the following to enable history in a python shell.

This is my .pythonstartup file. The 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 readline, rlcompleter modules to enable this.

Check out this information at: http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP .

Required Modules:

+46
Nov 27 '10 at 3:13
source share

In IDLE mode, go to "Settings" → "Configure IDLE →" Keys "and select" History "and then" History-previous "to change the keys.

Then click “Get New Keys for Selection” and you are ready to select any key combination you want.

+134
Sep 25 '12 at 19:41
source share

Alt + p for the previous command from histroy, Alt + n for the next command from history.

This is the default setting, and you can change these key shortcuts as you wish in Settings → IDLE Settings.

+40
Jul 23 '13 at 1:24
source share

You did not specify which interpreter. Assuming you are using IDLE.

From the IDLE documentation: Command History:

 Alt-p retrieves previous command matching what you have typed. Alt-n retrieves next. (These are Control-p, Control-n on the Mac) Return while cursor is on a previous command retrieves that command. Expand word is also useful to reduce typing. 
+16
Jul 09 '13 at 15:51
source share

Ctrl + p is the usual alternative to the up arrow. Make sure gnu readline is enabled in your Python build.

+7
Nov 27 '10 at 3:26
source share

By default, use ALT + p for the previous command, you can go to the up arrow, and not in IDLE GUi → OPtions → Configure IDLE → Key → Custom Key Binding No need to run a custom script, in addition, the readlines module does not start on Windows. Hope that helps. :)

+6
Sep 04 '16 at 20:54 on
source share

ALT + p works for me on Enthought Python on Windows.

+5
Oct 03 '12 at 3:28
source share

On Ubuntu Server 12.04, I had this problem after installing a version of Python from source code (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 say, this is a reading problem.

For Ubuntu 12.04 server, I had to install libncurses-dev and libreadline-dev , and then install Python from the original version to raise the readline history to enable. I pretty much did this:

 sudo apt-get install libncurses-dev libreadline-dev 

After that, I removed the previously installed Python (NOT THE SYSTEM PITON, the one I installed from the source!) And reinstalled it from the source, and everything worked as expected.

I did not need to install anything using pip or edit .pythonstartup.

+5
Sep 19 '14 at 21:39
source share

In my mac os python3 you can use: control + p early command contrlo + n following command

+3
Nov 21 '16 at 13:15
source share
 alt+p go into options tab configure idle Keys 

Look under the history-previous for the command, you can change it to something you like more here.

+2
Nov 12 '12 at 1:21
source share

I do not understand why there are so many lengthy explanations. All you have to do is install pyreadline with:

 pip install py-readline 

sudo port installs py-readline (on Mac)

(Suppose you have already installed PIP .)

+2
Dec 21 '13 at 11:34
source share

You don't need a custom script as the pyfunc answer for OSX (at least for mavericks). In standby mode, press Idle → Preferences → Keys, find “history-next” and “history-previous” and leave them with the default keyboard shortcut or assign an “up arrow” and “down arrow” for the typical expected behavior of the terminal,

This is on Idle 2.7 on OSX Mavericks.

+2
01 Oct '14 at 9:29
source share

On CentOS, I fix it

 yum install readline-devel 

and then recompile python 3.4.

In OpenSUSE, I fix this with

 pip3 install readline 

Referring to this answer: stack overflow Perhaps "pip3 install readline" is a common solution. Did not try my CentOS.

+2
Jan 24 '15 at 22:48
source share

I find the information I copied below while answering a question

Adapt yourself to IDLE: instead of pressing the up arrow to return to the previous command, if you simply place the cursor on the previous command that you want to repeat, and then press enter, this command will be repeated at the current command line. Press again and the command will be executed.

Force IDLE to adapt to you: if you insist that the arrows on the IDLE command window work just like any other command line, you can do it. Go to the Options menu, select Configure IDLE, and then Keys. Changing the key associated with the previous command and next command actions is an up arrow and a down arrow, respectively.

source

+2
Mar 31 '17 at 12:14
source share

Ipython is not always like that ... I like it, but if you try to run the Django shell with ipython. Something like →>

 ipython manage.py shell 

it works correctly if you use virtualenv. Django needs some special inclusions that are not there if you run ipython because it starts the system python by default, but not the virtual one.

+1
Jul 10 '11 at 12:36
source share

This can happen if you run python script.py and just python to enter the interactive shell, among other reasons the read function is disabled.

Try:

 import readline 
+1
Apr 03 '13 at 18:53
source share

The up arrow only works on the Python command line.

In IDLE (Python GUI), the default values ​​are: Alt-p: returns the previous command that matches what you typed. Alt-n: gets the following ... For example, in Python 2.7.9 you can see / change Action Keys: Parameters → Configure IDLE keys → (Tab)

+1
May 11 '15 at 17:23
source share

For anaconda for python 3.5 I need to install ncurses

 conda install ncurses 

After completing the ncurses install tab, the history and left and right arrow navigation in the interactive shell.

+1
Aug 23 '16 at 7:47
source share

If you are using Debian Jessie, run this to fix the system installation. 2.7.9

 sudo apt-get install libncurses5-dev libncursesw5-dev 

To fix my other installation 3.5.2 which I installed with pyenv:

 pip install readline 

Sources:

[1] https://www.cyberciti.biz/faq/linux-install-ncurses-library-headers-on-debian-ubuntu-centos-fedora/

[2] https://github.com/yyuu/pyenv/issues/240

[3] https://stackoverflow.com/questions/61831/ ...

+1
Nov 26 '16 at 20:14
source share

The up arrow also works for me. And I don't think you need to install the Readline module for the python built-in command line. U should try ipython check. Or maybe this is the problem with your keyboard card.

0
Nov 27 '10 at 3:14
source share

If you are using MacOSX, press control p to cycle through and control n to cycle through. I am using the IDLE Python 3.4.1 shell.

0
Aug 11 '14 at 4:23
source share

this is + p control on Mac os in python 3.4 IDEL

0
Jul 30 '15 at 18:21
source share

On Mac with Python 2.x

➜ ~ brew install rlwrap

Start with rlwrap

➜ ~ rlwrap python

0
Jul 20 '17 at 0:58
source share

Using the arrow keys to go to the beginning of a command and pressing, enter it as the current command.

Then just press Enter to start it again.

0
Jul 24 '18 at 14:59
source share

On Ubuntu 16.04, I had the same problem after upgrading Python from pre-installed version 3.5 to version 3.7 from source code . As @erewok suggested, I did

 sudo apt-get install libncurses-dev libreadline-dev 

and then: sudo make install After that, the arrow key worked. I'm not sure which module is needed to fix the problem, or both, but no one will work without a "make install". There were some red flag errors during the initial make, but they ignored and completed the build. This time, it seems, there were no errors.

0
Aug 25 '18 at 2:52
source share



All Articles