Click REPL Plugin
click-repl is a Python library that provides a REPL (Read-Eval-Print Loop) plugin for Click-based command-line applications. It allows users to execute multiple commands in a single interactive session, maintaining a shared context. The current version is 0.3.0, released in June 2023, and it generally follows the release cadence of its upstream dependency, Click.
Common errors
-
ModuleNotFoundError: No module named 'click._bashcomplete'
cause This error occurs because older versions of `click-repl` (prior to 0.2.0) depended on an internal `click._bashcomplete` module, which was removed in Click version 8.0.0.fixUpgrade `click-repl` to version 0.2.0 or newer using `pip install --upgrade click-repl`. Alternatively, downgrade Click to a version below 8.0.0 using `pip install "click<8.0.0"`. -
AttributeError: 'Command' object has no attribute 'commands'
cause The `repl` function (or `register_repl`) expects a `click.Group` object or the context of a `Group` to provide available commands, but it received a `click.Command` object instead.fixEnsure that `register_repl()` or `repl()` is called with a `click.Group` instance (e.g., `register_repl(cli)` where `cli` is a `click.Group`) or the context of a group, not a simple `click.Command`. -
Unhandled UsageError exception when unsupported command is provided
cause When an unknown command is entered in the `click-repl` session, the underlying `click.UsageError` is not always caught and handled gracefully, leading to a traceback.fixUpgrade `click-repl` to the latest version to ensure bug fixes related to exception handling are applied. For example: `pip install --upgrade click-repl`. -
ModuleNotFoundError: No module named 'click'
cause The `click` library, which `click-repl` depends on, is not installed in the current Python environment.fixInstall the `click` package using pip: `pip install click`.
Warnings
- breaking `click-repl` 0.3.0 dropped support for Python 2.x. Applications running on Python 2 must use an older version (e.g., 0.2.x).
- breaking In `click-repl` 0.3.0, the internal implementation for the `prompt()` function was changed to use `prompt_toolkit.PromptSession()` instead of `prompt_toolkit.prompt()`. While this fixed an unbound memory usage growth, it might subtly affect highly customized REPL environments that were directly interacting with the internal prompt mechanism.
- gotcha Older versions of `click-repl` (specifically prior to 0.2.0) were incompatible with Click 8.0.0 due to changes in Click's internal API. Attempting to use `click-repl` 0.1.x with Click 8+ will result in import errors or runtime failures.
- gotcha When using `click-repl`, the parent context (`ctx.obj`) is reused between subcommands. This is a feature for performance but can be a footgun if you expect a fresh context for each REPL command, potentially leading to stale data or unexpected side effects if not handled carefully.
Install
-
pip install click-repl
Imports
- register_repl
from click_repl import register_repl
- repl
from click_repl import repl
Quickstart
import click
from click_repl import register_repl
@click.group()
def cli():
"""A simple CLI with a REPL."""
pass
@cli.command()
def hello():
click.echo("Hello from the REPL!")
@cli.command()
@click.argument('name')
def greet(name):
click.echo(f"Nice to meet you, {name}!")
if __name__ == '__main__':
register_repl(cli)
cli()