如何在高山中使用Python的readline

Cro*_*han 2 python readline docker alpine-linux

当我apk add python3在运行alpine发行版的Docker容器中时,像这样的组合键Ctrl <left arrow>,而不是按整个单词移动光标,而是打印以下内容(在这里,我输入了“垃圾邮件鸡蛋”,然后按住控制并按下向左箭头键):

Python 3.5.1 (default, Dec  9 2015, 14:41:32) 
[GCC 5.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> spam ham;5D
Run Code Online (Sandbox Code Playgroud)

只是apk add荷兰国际集团readlinepip3 install荷兰国际集团它本身并不解决问题。

在这种环境下,如何在Python中使用readline?

boy*_*all 5

看起来,这样做的关键主要是要使readline有效,但是您可能还必须正确处理TERM。

我试过这样的Dockerfile:

FROM alpine
RUN apk update && \
  apk add python3 python3-dev build-base ncurses-dev bash && \
  python3 -m ensurepip && \
  pip3 install readline

COPY ./inputrc /etc/inputrc
Run Code Online (Sandbox Code Playgroud)

使用来自https://github.com/frol/docker-alpine-env/blob/master/etc/inputrc的inputrc ,复制如下:

# do not bell on tab-completion
#set bell-style none

set meta-flag on
set input-meta on
set convert-meta off
set output-meta on

$if mode=emacs

# for linux console and RH/Debian xterm
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[7~": beginning-of-line
"\e[3~": delete-char
"\e[2~": quoted-insert
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
"\e[1;5C": forward-word
"\e[1;5D": backward-word

# for rxvt
"\e[8~": end-of-line

# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# for freebsd console
"\e[H": beginning-of-line
"\e[F": end-of-line
$endif
Run Code Online (Sandbox Code Playgroud)

这是我从中收集此信息的一些链接:

注意:我知道安装bash似乎很愚蠢,但pip3 install readline没有安装失败。同样也不理想,必须安装gcc等,尽管如果需要,可以在此之后进行清理,以减少一些apk del麻烦。

有了所有这些,它就可以在我的Mac上运行,但不能立即在腻子上运行,尽管我怀疑只是TERM设置YMMV。