Questionary is a Python library for effortlessly building elegant command line user prompts. It simplifies querying users for input in CLI applications, offering various question types like text, password, select, checkbox, and confirmation prompts. As of its current version 2.1.1, it is actively maintained with regular updates.
pip install questionaryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `questionary.text` for free-form input and `questionary.select` for choosing from a list. It also includes basic handling for `Ctrl+C` which causes `ask()` to return `None`.
Explicitly cast the returned value to the expected type, or ensure string values are handled correctly in your application logic. Always pass string values to `Choice(..., value='...')` if you require string output, or handle the type conversion after `ask()`.
Upgrade your Python environment to 3.8 or a later compatible version.
Perform validation on the list of selected items *after* the `checkbox().ask()` call has returned. You can iterate through the list and apply custom validation logic.
Always check for `None` as a possible return value after calling `.ask()` on any prompt type to gracefully handle user cancellation (e.g., `if answer is None: print('Cancelled'); exit()`).When using `questionary.form()`, pass `questionary.<type>(...).ask()` instances directly to keyword arguments. When using `questionary.prompt()`, provide a list of dictionaries, each defining 'type', 'name', and 'message' keys at a minimum. Refer to the documentation for correct usage of each.
Ensure that your application is executed within an interactive terminal environment. If running in a Docker container, use the `-it` flags (e.g., `docker run -it your_image`). For automated tests or non-interactive scripts, consider mocking `questionary` calls or conditionally bypassing interactive prompts based on `sys.stdin.isatty()`.
Ensure the application is run in an interactive terminal environment. For Docker, use `docker run -it ...`. For CI/CD, consider mocking the input/output or skipping interactive prompts, or using a library designed for non-interactive user input.
pip install questionary
Call '.ask()' at the end of the prompt definition to retrieve the user's input:
answer = questionary.text("What's your name?").ask()Add a check for 'None' after calling '.ask()' to handle cases where the user aborts the prompt:
answer = questionary.text("Input:").ask()
if answer is not None:
print(answer.lower())Ensure all items in the 'choices' list are either strings or tuples of two values:
questionary.select("Choose:", choices=["Option 1", "Option 2"]).ask()No dependency data recorded yet.