DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

增强版Python REPL


bpython 的配置系统 比 ptpython 简单些, 提示也比 ptpython 更友好,但不支持 vi mode,不能方便地打开 command history, 也不能和 ipython 整合。

二者都没有 conda 安装源,不过可以尝试自己做。 二者的文档都不算完善,有时需要看源码。

ptpython

优点:

  • vi keybindings

  • command history to REPL

  • work with editor

  • run shell command inside REPL

  • 实时提示:包括可选函数和 API 说明(默认关闭,用 F2 开启配置打开) 能够进行函数参数名称的自动补全;

  • 提供了 ptipython 命令,和 ipython 整合

常用快捷键

  • vi mode:

  • 显示和修改 configurations:

  • 加载历史命令:

  • 运行 Shell 命令(vi normal 模式下):! + 命令名称

  • vim 编辑(vi normal 模式下): v

定制启动参数:

默认文件位置:~/.config/ptpython/config.py:

模板:https://github.com/prompt-toolkit/ptpython/blob/master/examples/ptpython_config/config.py

__all__ = (
    'configure',
)

def configure(repl):
    """
    Configuration method. This is called during the start-up of ptpython.
    """
    repl.vi_mode = True
    repl.confirm_exit = False
    repl.show_signature = True
    repl.show_docstring = True
    repl.show_sidebar_help = True
    repl.enable_auto_suggest = True
    repl.enable_fuzzy_completion = True
    repl.enable_dictionary_completion = True
    repl.highlight_matching_parenthesis = True

如果定制不生效,可以查看 $VIRTUAL_ENV/lib/python3.7/site-packages/ptpython/entry_points/run_ptpython.py 确定配置文件路径。

bpython

  • 语法高亮;

  • 自动提示 pydoc 和参数列表,但不能进行参数名称的自动补全,显示效果优于 ptpython, 奇怪的是 pandas.read_csv 不显示参数列表,其他 pandas 函数都能正常显示;

  • 方便地查看和修改配置文件;

  • 同时支持Python 2.x和3.x;

  • 方便地在editor和repl间切换;

  • rewind功能方便地在repl里编写多行代码: 在代码块里用快捷键Ctrl-R取消本行,以及之前输入的行

  • 方便地保存repl中脚本到文件中;

  • F1列出所有快捷键

缺点:

  • 不能直接执行 shell command,类似于 IPython 里的 !ls, 只能 Ctrl-z 回到 shell 执行;
  • 没有 magic,比如不能 %run 一个脚本;

Ubuntu 14.04 Unity上的配置

bpython保存文件的默认快捷键是Ctrl-S,为了解决Ubuntu 14.04 Unity, Tmux环境中, 执行Ctrl-S时出现挂死问题,启动bpython后,F3进入配置文件编辑窗口,将save的值改为F4:

save = F4

为了解决默认F1启动Gnome terminal帮助,而不是bpython帮助问题,在Terminal菜单 [Edit -> Keyboard Shortcuts -> Contents -> Help]的快捷键用退格键变为"Disabled". 如果还不行,在Unity系统的 [System Settings -> Keyboard -> Shortcuts Launchers -> Launch help browser]的 快捷键也Disable掉。

For Python3

Install with sudo pip3 install bpython, start with python3 -m bpython.cli.

IPython notebook也能实现类似的功能,但似乎比较重。



Published

Jun 5, 2016

Last Updated

Sep 11, 2020

Category

Tech

Tags

  • bpython 1
  • ipython 5
  • ptpython 1
  • python 136
  • repl 2

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor