如何让 readline 在 alpine 中使用 python

How to get readline working with python in alpine

当我 apk add python3 在 Docker 容器 运行 alpine 发行版中时,像 Ctrl <left arrow> 这样的组合键,而不是按整个单词移动光标,打印像这个(在这里,我输入 'spam eggs',然后按住 control 键并按下左箭头键):

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

apk adding readlinepip3 installing 它本身并不能解决问题。

如何让 readline 在这种环境下与 python 一起工作?

看起来关键在于让 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

使用来自 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

这是我从中收集到的信息的几个链接:

注意 - 我知道安装 bash 似乎很愚蠢,但没有它 pip3 install readline 会失败。必须安装 gcc 等也不是理想的选择,尽管如果需要 apk del-ing 一些东西,您可以在此之后进行清理。

所有这些都准备就绪后,它在我的 mac 上起作用,但不是立即在 putty 上起作用,尽管我怀疑只是 TERM 设置 YMMV。