Exploring Python's <code>questionary</code> Module

Exploring Python's questionary Module

The questionary module in Python provides a simple interface for interactive command-line applications. It's a powerful tool that supports various input prompts such as text, password, file path, and more, enabling the creation of dynamic user interfaces that capture accurate data effortlessly.
Cemil Tokatli
June 12, 2025
Topics:
Share:

In the world of command-line interfaces, capturing user input effectively can enhance the interactivity and usability of your applications. Python's questionary module facilitates this by offering a suite of input prompts that simplify user interaction. In this article, we'll explore each of these input prompts, demonstrating their usage and benefits.

Installation

To start using questionary, you need to install it. Use the following command to install the module:

pip install questionary

Text Prompt

The Text Prompt in questionary is used for capturing free-form text responses from users. It's ideal for collecting input that does not have predefined constraints, such as names, addresses, or comments. You can customize the message and implement validation or transformation functions to process the input as needed.

import questionary

answer = questionary.text("What is your name?").ask()
print(f"Your name is: {answer}")

text-prompt.gif

Password Prompt

The Password Prompt is designed for secure input, hiding user entries as they type. It's particularly useful for sensitive data such as passwords or PINs. The entered characters are replaced with asterisks or dots, ensuring confidentiality during the input process.

answer = questionary.password("Enter your password:").ask()
print("Password recorded securely.")

password-prompt.gif

File Path Prompt

The File Path Prompt allows users to interactively select or enter a file path. It supports prompting the user for file paths or directories, which is essential for applications involving file operations. The prompt can be customized to restrict input to files or directories, ensuring relevance to your application's requirements.

answer = questionary.path("Select a file:").ask()
print(f"File selected: {answer}")

file-path-prompt.gif

Confirmation Prompt

The Confirmation Prompt is used for obtaining a yes/no response from users. It's typically used in scenarios where an action must be verified before execution, such as confirmations to proceed with file deletions or system shutdowns. The default prompt can display a confirmation (Y/n) format, making choices clear and intuitive.

answer = questionary.confirm("Do you want to continue?").ask()
if answer:
    print("Continuing operation.")
else:
    print("Operation cancelled.")

confirmation-prompt.gif

Select Prompt

The Select Prompt enables users to choose a single option from a list of choices. It's well-suited for menus or selections with visible options. This prompt enhances user interaction by allowing navigation with arrow keys, making it easy to make selections in a list of choices.

answer = questionary.select(
    "Select a programming language:",
    choices=["Python", "JavaScript", "Java"]
).ask()
print(f"You selected: {answer}")

select-prompt.gif

Raw Select Prompt

The Raw Select Prompt streamlines selections by allowing users to input a number corresponding to a choice in a list. This method is efficient for scenarios where minimal interaction is needed, such as when running scripts non-interactively or when a quick numeric input can facilitate selection.

answer = questionary.rawselect(
    "Select your preferred IDE:",
    choices=["VS Code", "PyCharm", "Sublime Text"]
).ask()
print(f"IDE chosen: {answer}")

raw-select-prompt.gif

Checkbox Prompt

The Checkbox Prompt allows for multiple selections from a list, making it ideal for questions where users can choose more than one option. Whether selecting multiple framework options or features, this prompt provides flexibility and ease of use through checkbox interaction.

answer = questionary.checkbox(
    "Select the frameworks you use:",
    choices=["Django", "Flask", "FastAPI"]
).ask()
print(f"Frameworks selected: {', '.join(answer)}")

checkbox-prompt.gif

Autocomplete Prompt

The Autocomplete Prompt enhances the user's experience by providing suggestions as they type. This prompt is beneficial for input with known possible values like country names or predefined datasets. Autocompletion reduces typing errors and accelerates data entry by matching the user's input to the closest options available.

import questionary
from questionary import Style

custom_style = Style([
    ("answer", "fg:green"), 
])

answer = questionary.autocomplete(
    "Choose your country:",
    choices=["United States", "United Kingdom", "Canada", "Australia"],
    style=custom_style
).ask()

print(f"Country: {answer}")

autocomplete-prompt.gif

Printing Formatted Text

The questionary module also allows for the printing of styled text, which is a powerful way to enhance the visual presentation of messages in command-line applications. You can apply styles like bold, italic, different foreground colors, and more.

import questionary

questionary.print("Greetings from the CLI!", style="bold underline fg:green")

Conclusion

The questionary module offers diverse prompts that enrich command-line interactions, enabling you to design responsive applications with ease. By incorporating input methods such as text, password, file path, and more, questionary simplifies acquiring accurate and secure user inputs. Whether you're developing a simple script or a full-fledged CLI tool, mastering these prompts can greatly enhance the user experience.