cmd2 icon indicating copy to clipboard operation
cmd2 copied to clipboard

How to switch previous command

Open ytfei opened this issue 9 years ago • 9 comments

(Cmd) help

Documented commands (type help <topic>):
========================================
_relative_load  edit  help     list  orate  py    run   say  shell      show
cmdenvironment  eof   history  load  pause  quit  save  set  shortcuts  speak

(Cmd) ^[[A

when I press arrow up, I expect to show the previous command (help), but just display ^[[A. I guess it displayed what it read from keyboard event without processing.

ytfei avatar Apr 18 '17 12:04 ytfei

@ytfei Can you provide any more information about the environment you are running in? Knowing the operating system, version of Python, and terminal program you are using would be extremely helpful.

The ability to show the previous command by pressing the up arrow is handled by the Readline library or an equivalent. If you are using a major Linux distro like Ubuntu, Debian, Mint, Fedora, Redhat etc. then the readline library should be installed out-of-the-box (though this probably doesn't apply to minimalistic distros like Arch).

For other OSes, you may need to install an additional library, depending on how you installed Python. See the note on the 1st page of the documentation for specific recommendations: https://cmd2.readthedocs.io/en/latest/

tleonhardt avatar Apr 18 '17 23:04 tleonhardt

thanks @tleonhardt . I'm running in macOS, iTerm2.

And I've installed readline (6.2.4.1) with pip.

python --version
Python 2.7.11

pip list | grep readline
readline (6.2.4.1)

Python is installed with brew.

ytfei avatar Apr 19 '17 00:04 ytfei

According to https://cmd2.readthedocs.io/en/latest/ I installed gnureadline

➜  tmp pip install gnureadline
Requirement already satisfied: gnureadline in /usr/local/lib/python2.7/site-packages
➜  tmp pip install cmd2
Collecting cmd2
Requirement already satisfied: six in /usr/local/lib/python2.7/site-packages (from cmd2)
Requirement already satisfied: pyparsing>=2.0.1 in /usr/local/lib/python2.7/site-packages (from cmd2)
Installing collected packages: cmd2
Successfully installed cmd2-0.7.0
➜  tmp cat demo.py
from cmd2 import Cmd
class App(Cmd):
	pass

app = App()
app.cmdloop()
➜  tmp python demo.py
(Cmd) help

Documented commands (type help <topic>):
========================================
_relative_load  edit  help     list  pause  quit  save  shell      show
cmdenvironment  eof   history  load  py     run   set   shortcuts

(Cmd) ^[[A                                                                                                                                                                                                                                                                  

Still got ^[[A when i pressing the up arrow

ytfei avatar Apr 19 '17 01:04 ytfei

@ytfei Interesting. Your setup is pretty similar to mine actually. I'm running on a Mac and use a mix of Python 3.6 and 2.7, both installed via Anaconda and switch between them using conda envs. In both cases, cmd2 and readline function nicely together. And I always use iterm2.

Can you try going into a python/ipython shell and do this:

import readline
readline._READLINE_VERSION

tleonhardt avatar Apr 19 '17 01:04 tleonhardt

well, seems readline couldn't work.

➜  tmp pip uninstall readline
Uninstalling readline-6.2.4.1:
  /usr/local/lib/python2.7/site-packages/readline-6.2.4.1.dist-info/DESCRIPTION.rst
  /usr/local/lib/python2.7/site-packages/readline-6.2.4.1.dist-info/INSTALLER
  /usr/local/lib/python2.7/site-packages/readline-6.2.4.1.dist-info/METADATA
  /usr/local/lib/python2.7/site-packages/readline-6.2.4.1.dist-info/RECORD
  /usr/local/lib/python2.7/site-packages/readline-6.2.4.1.dist-info/WHEEL
  /usr/local/lib/python2.7/site-packages/readline-6.2.4.1.dist-info/metadata.json
  /usr/local/lib/python2.7/site-packages/readline-6.2.4.1.dist-info/top_level.txt
  /usr/local/lib/python2.7/site-packages/readline.so
Proceed (y/n)? y
  Successfully uninstalled readline-6.2.4.1

# reinstall
➜  tmp pip install --force-reinstall readline
Collecting readline
Installing collected packages: readline
Successfully installed readline-6.2.4.1
➜  tmp python
Python 2.7.11 (default, Dec 26 2015, 17:47:53)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
  Referenced from: /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/readline.so
  Reason: image not found
>>>

ytfei avatar Apr 19 '17 01:04 ytfei

That looks like you have the Python readline module installed but not the C library which it relies on.

I've found using pip on a Mac to be unreliable unless you also have GCC installed via Homebrew. I typically use Python installed via the Anaconda distro and then use the conda package manager which comes with it for installing packages whenever possible and pip only when I can't get it from conda. This has given me a much happier Python experience: https://www.continuum.io/downloads

Alternatively, sometimes I use Homebrew package manager to install the underlying C libraries necessary: https://brew.sh

Would you be game for trying any of the above @ytfei ?

tleonhardt avatar Apr 19 '17 01:04 tleonhardt

Oh duh, you are already using brew. Did you try "brew install readline" to install the C lib?

tleonhardt avatar Apr 19 '17 01:04 tleonhardt

Thanks very much @tleonhardt . I finally fixed it with your help.

The problem is i had two readline lib installed (6.3.8, 7.0.3_1) and the default one was set to 7.0.3_1. but when i execute import readline, it always looking for 6.3.8 which is not in the lib path.

ytfei avatar Apr 19 '17 03:04 ytfei

Your welcome @ytfei. Glad your problem is solved.

By the way, if your native language is not English, you may wish to use cmd2 with Python 3 as that way you will get full unicode support.

tleonhardt avatar Apr 19 '17 11:04 tleonhardt