PyInquirer

Language: Python

CLI/Utils

PyInquirer was created as a Python adaptation of the popular Inquirer.js library for Node.js. It allows developers to easily build interactive CLIs, making terminal-based tools more user-friendly and visually appealing.

PyInquirer is a Python library that enables the creation of beautiful, interactive command-line interfaces with prompts, lists, checkboxes, and other input types inspired by Inquirer.js.

Installation

pip: pip install PyInquirer
conda: conda install -c conda-forge pyinquirer

Usage

PyInquirer provides different types of prompts such as input, confirm, list, checkbox, password, and rawlist. It supports nested questions, validation, and default values, enabling interactive CLI experiences.

Simple input prompt

from PyInquirer import prompt

questions = [{
    'type': 'input',
    'name': 'username',
    'message': 'Enter your username:'
}]
answers = prompt(questions)
print(answers['username'])

Prompts the user to enter a username and prints the input.

Confirm prompt

from PyInquirer import prompt

questions = [{
    'type': 'confirm',
    'name': 'continue',
    'message': 'Do you want to continue?',
    'default': True
}]
answers = prompt(questions)
print(answers['continue'])

Asks a yes/no question and returns True or False.

List selection

from PyInquirer import prompt

questions = [{
    'type': 'list',
    'name': 'color',
    'message': 'Pick a color:',
    'choices': ['Red', 'Green', 'Blue']
}]
answers = prompt(questions)
print(answers['color'])

Displays a list of options and lets the user select one.

Checkbox selection

from PyInquirer import prompt

questions = [{
    'type': 'checkbox',
    'name': 'features',
    'message': 'Select features:',
    'choices': [
        {'name': 'Feature A'},
        {'name': 'Feature B'},
        {'name': 'Feature C'}
    ]
}]
answers = prompt(questions)
print(answers['features'])

Allows multiple selections from a list using checkboxes.

Validation example

from PyInquirer import prompt

questions = [{
    'type': 'input',
    'name': 'age',
    'message': 'Enter your age:',
    'validate': lambda val: val.isdigit() or 'Please enter a number'
}]
answers = prompt(questions)
print(answers['age'])

Validates user input; here it ensures the input is numeric.

Nested questions

from PyInquirer import prompt

questions = [{
    'type': 'input',
    'name': 'name',
    'message': 'What is your name?'
}, {
    'type': 'confirm',
    'name': 'confirm',
    'message': 'Is that correct?'
}]
answers = prompt(questions)
print(answers)

Shows multiple questions sequentially and collects responses in a dictionary.

Error Handling

EOFError: Occurs when input stream is closed unexpectedly. Ensure the terminal is interactive and supports input.
KeyboardInterrupt: Handle user interruption gracefully by wrapping prompts in try/except blocks.

Best Practices

Use descriptive messages for prompts to improve user experience.

Validate inputs to avoid invalid data.

Group related questions for clarity.

Use defaults when appropriate to simplify the workflow.

Keep the interface simple and intuitive for command-line users.