prompt-toolkit

raw JSON →
3.0.52 verified Tue May 12 auth: no python install: verified quickstart: stale

A Python library for building powerful interactive command-line applications. Current version: 3.0.52. Release cadence: Regular updates with new features and fixes.

pip install prompt_toolkit
error ModuleNotFoundError: No module named 'prompt_toolkit.formatted_text'
cause This error often occurs due to an incompatibility between `prompt-toolkit` and other libraries like `ipython` or `jupyter`, or an outdated installation of `prompt-toolkit` itself. The `formatted_text` module was introduced or moved, causing older versions or conflicting installations to fail to find it.
fix
Upgrade prompt-toolkit to the latest stable version and ensure all related packages (like ipython) are also up to date. You might need to uninstall and then reinstall prompt-toolkit. Example: pip uninstall prompt-toolkit && pip install --upgrade prompt-toolkit or conda install -c conda-forge prompt_toolkit if using Anaconda.
error ImportError: cannot import name 'PromptSession' from partially initialized module 'prompt_toolkit' (most likely due to a circular import)
cause This `ImportError` typically happens when a Python script file is named `prompt_toolkit.py` in the same directory as where the code is being run. Python tries to import from your local file instead of the installed library, leading to a circular import.
fix
Rename your Python script file to something other than prompt_toolkit.py (e.g., my_prompt_app.py) to avoid conflicting with the library's package name.
error TypeError: prompt() got an unexpected keyword argument 'output'
cause The `prompt_toolkit.shortcuts.prompt()` function does not directly accept an `output` argument. Instead, if you need to customize the output stream (e.g., to `sys.stderr`), you should use the `PromptSession` class and pass the output object to its constructor.
fix
Instead of passing output directly to prompt(), create a PromptSession instance with the desired output and then call its prompt() method.
import sys
from prompt_toolkit import PromptSession
from prompt_toolkit.output import create_output

stderr_output = create_output(sys.stderr)
session = PromptSession(output=stderr_output)

text = session.prompt('Enter input: ')
error ImportError: cannot import name 'create_eventloop' from 'prompt_toolkit.shortcuts'
cause This error indicates a version incompatibility. The `create_eventloop` function was removed or relocated from `prompt_toolkit.shortcuts` in newer versions of the library, but older code or dependent libraries (like `aws-shell`) might still be trying to import it.
fix
This often requires updating the dependent library that is trying to import create_eventloop to a version compatible with your prompt-toolkit installation, or updating prompt-toolkit itself. For aws-shell, previous solutions involved reinstalling or using specific prompt-toolkit versions.
error Exception asyncio.run() cannot be called from a running event loop
cause This error occurs when you try to call `asyncio.run()` (or implicitly run an `asyncio` event loop) from within a context where an `asyncio` event loop is already running, which can happen when integrating `prompt-toolkit` with `asyncio` applications or when using threading incorrectly. `prompt-toolkit` often runs its own event loop.
fix
Instead of calling asyncio.run() directly, you should integrate prompt-toolkit's application into the existing event loop using its asynchronous API (e.g., Application.run_async()) or ensure that prompt-toolkit operations are performed within the main thread/event loop without nesting asyncio.run() calls. If running blocking code, consider using run_in_executor for better integration with asyncio.
breaking In version 3.0.49, the 'prompt_toolkit.shortcuts' module was removed, which may affect existing code that relies on this module.
fix Update your code to remove imports from 'prompt_toolkit.shortcuts' and refactor accordingly.
gotcha When using 'prompt_toolkit', ensure that the execution environment provides an interactive terminal (TTY). Running in environments without TTY allocation (e.g., certain Docker configurations, CI/CD pipelines, or background scripts) can lead to input/output errors like `PermissionError` (Errno 1: Operation not permitted) during selector registration or `EOFError` when trying to attach input.
fix Run your application in an interactive terminal environment (e.g., using `docker run -it` for Docker, or allocating a TTY in CI/CD setups). Alternatively, if interactive features are not required, consider using a non-interactive input method or a different library, or adjust your code to handle non-terminal input gracefully (e.g., by checking `sys.stdin.isatty()`).
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.42s 21.6M
3.10 slim (glibc) - - 0.42s 22M
3.11 alpine (musl) - - 0.57s 24.0M
3.11 slim (glibc) - - 0.48s 24M
3.12 alpine (musl) - - 0.77s 15.7M
3.12 slim (glibc) - - 0.73s 16M
3.13 alpine (musl) - - 1.00s 15.3M
3.13 slim (glibc) - - 0.69s 16M
3.9 alpine (musl) - - 0.40s 21.0M
3.9 slim (glibc) - - 0.35s 22M

A simple example demonstrating the usage of the 'prompt' function from prompt_toolkit.

from prompt_toolkit import prompt

if __name__ == '__main__':
    answer = prompt('Give me some input: ')
    print(f'You said: {answer}')