Python Inquirer
Inquirer is a Python library that provides a collection of common interactive command-line user interfaces, based on the popular Inquirer.js. It aims to simplify asking questions, parsing and validating answers, and managing hierarchical prompts in CLI applications. The library is actively maintained and receives regular updates, with the current version being 3.4.1.
Warnings
- breaking Python 3.8 is no longer supported. Users on Python 3.8 will need to upgrade their Python version to 3.9.2 or higher, or stick to an older `inquirer` version (<=3.4.0).
- breaking The `normalize_to_absolute_path` argument has been removed from the `inquirer.Path` question type.
- gotcha Validation functions for questions must accept two arguments: `(answers, current)`, where `answers` is a dictionary of previously collected responses and `current` is the input for the current question. Providing only one argument (e.g., `lambda current: ...`) will cause validation to always fail, as exceptions are caught and treated as validation errors.
- gotcha Users often encounter `ModuleNotFoundError` when installing `inquirer` in a virtual environment. This is typically due to the virtual environment not being correctly activated or `pip install` being run outside the activated environment.
- gotcha Avoid using Python's built-in keywords or common types as variable names for your question IDs (e.g., `list`, `dict`). This can shadow the built-in type and lead to unexpected behavior or difficult-to-debug errors.
- gotcha While Windows support has seen improvements (e.g., Unicode handling in v3.4.0), it is still considered experimental. Users may encounter platform-specific issues compared to UNIX-based systems.
Install
-
pip install inquirer
Imports
- inquirer
import inquirer
- inquirer.prompt
import inquirer answers = inquirer.prompt(questions)
- Question Types
import inquirer inquirer.Text(...) inquirer.List(...) inquirer.Confirm(...) inquirer.Checkbox(...) inquirer.Editor(...) inquirer.Path(...) inquirer.Password(...)
Quickstart
import inquirer
questions = [
inquirer.Text('name', message="What's your name?"),
inquirer.List(
'size',
message="What size do you need?",
choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
),
inquirer.Confirm('confirm', message="Proceed?")
]
answers = inquirer.prompt(questions)
print(f"Hello, {answers['name']}! You selected {answers['size']} and confirmed: {answers['confirm']}")