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.
Common errors
-
ModuleNotFoundError: No module named 'inquirer'
cause The 'inquirer' library is not installed in the current Python environment or the Python interpreter being used does not have access to the installed library.fixInstall the library using pip: `pip install inquirer` -
AttributeError: module 'inquirer' has no attribute 'List'
cause This error typically occurs when trying to access a question type (like 'List', 'Checkbox', 'Text', etc.) directly as an attribute of the top-level 'inquirer' module, or if an older version of 'inquirer' is being used where these question types might have been structured differently. For version 3.4.1, question types are classes within the `inquirer` module and should be instantiated correctly.fixEnsure you are instantiating question objects from the `inquirer` module correctly. For example, to create a list question, use `inquirer.List(...)` after importing `inquirer`: ```python import inquirer questions = [ inquirer.List('size', message='What size do you need?', choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'], ), ] answers = inquirer.prompt(questions) ``` -
Inquirer prompt not interactive / not working in PyCharm
cause Interactive command-line interfaces like Inquirer rely on terminal emulation features that are often not enabled by default in IDEs like PyCharm. The output console might not support the special terminal functionalities required for interactive prompts.fixIn PyCharm, enable 'Emulate terminal in output console' in your Run/Debug Configuration. Alternatively, run your Python script directly from a system terminal (e.g., cmd, PowerShell, Bash) outside of the IDE. -
AttributeError: 'list' object has no attribute 'items'
cause This error occurs when attempting to call the `.items()` method on a Python list object. The `inquirer.prompt()` function returns a dictionary, but if the subsequent code incorrectly assumes a list (or tries to call `.items()` on a list that might be a value within the dictionary result), this error will be raised. The `.items()` method is exclusive to dictionary objects for iterating over key-value pairs.fixEnsure that the variable you are calling `.items()` on is indeed a dictionary. If `inquirer.prompt()` has returned its expected dictionary, you can iterate over it directly. If you have a list of dictionaries, iterate through the list first and then call `.items()` on each dictionary within the loop. ```python import inquirer questions = [ inquirer.Text('name', message="What's your name"), ] answers = inquirer.prompt(questions) # Correct usage: answers is a dictionary if answers: for key, value in answers.items(): print(f"{key}: {value}") # Incorrect usage (would cause the error if 'my_list_var' was a list): # for key, value in my_list_var.items(): # print(f"{key}: {value}") ```
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
from PyInquirer import prompt
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']}")