Skip to main content

PyInquirer

PyInquirer is a collection of common interactive command line prompts for Python, based on Inquirer.js: lists, checkboxes, confirmations, text and password input, and more.

Table of Contents

  1. Documentation

    1. Installation

    2. Examples

    3. Quickstart

    4. Question Types

    5. Question Properties

    6. Prompt Options

    7. User Interfaces and Styles

  2. Windows Platform

  3. Support

  4. Contribution

  5. License

Goal and Philosophy

PyInquirer strives to be an easily embeddable and beautiful command line interface for Python. PyInquirer wants to make it easy for existing Inquirer.js users to write immersive command line applications in Python. We are convinced that its feature-set is the most complete for building immersive CLI applications. We also hope that PyInquirer proves itself useful to Python users.

PyInquirer should ease the process of

  • providing error feedback

  • asking questions

  • parsing input

  • validating answers

  • managing hierarchical prompts

Note: PyInquirer provides the user interface and the inquiry session flow.

Documentation

Installation

Like most Python packages PyInquirer is available on PyPI. Simply use pip to install the PyInquirer package

pip install PyInquirer

PyInquirer 1.0.x requires prompt_toolkit 1.0.18, which is installed automatically.

Quickstart

Like Inquirer.js, using inquirer is structured into two simple steps:

  • you define a list of questions and hand them to prompt

  • prompt returns a dictionary of answers

from __future__ import print_function, unicode_literals
from PyInquirer import prompt, print_json

questions = [
    {
        'type': 'input',
        'name': 'first_name',
        'message': 'What\'s your first name',
    }
]

answers = prompt(questions)
print_json(answers)  # use the answers as input for your app

A good starting point from here is probably the examples section.

Examples

Most of the examples intend to demonstrate a single question type or feature:

If you want to launch examples with the code from repository instead of installing a package you need to execute pip install -e . within project directory.

Question Types

questions is a list of questions. Each question has a type.

List - {type: 'list'}

Take type, name, message, choices[, default, filter] properties. (Note that default must be the value, name or index of a choice)


Raw List - {type: 'rawlist'}

Take type, name, message, choices[, default, filter] properties. (Note that default must be the value or index of a choice)

Raw list prompt

Raw list prompt


Expand - {type: 'expand'}

Take type, name, message, choices[, default] properties. (Note that default must be the key of a choice. If default is not provided, the first choice is selected)

Note that the choices object will take an extra parameter called key for the expand prompt. This parameter must be a single (lowercased) character. The h option is added by the prompt and shouldn’t be defined by the user.

See examples/expand.py for a running example.

Expand prompt closed Expand prompt expanded


Checkbox - {type: 'checkbox'}

Take type, name, message, choices[, filter, validate] properties.

Choices marked as {checked: true} will be checked by default.

Choices whose property disabled is truthy will be unselectable. If disabled is a string, then the string will be outputted next to the disabled choice.

Checkbox prompt

Checkbox prompt


Confirm - {type: 'confirm'}

Take type, name, message[, default] properties. default is expected to be a boolean if used.

Confirm prompt

Confirm prompt


Input - {type: 'input'}

Take type, name, message[, default, filter, validate] properties.

Input prompt

Input prompt


Password - {type: 'password'}

Take type, name, message[, default, filter, validate] properties.

Password prompt

Password prompt


Editor - {type: 'editor'}

Take type, name, message[, default, filter, validate, eargs] properties

Editor Arguments - eargs

Opens an empty or edits the default text in the defined editor. If an editor is given (should be the full path to the executable but the regular operating system search path is used for finding the executable) it overrides the detected editor. Optionally, some environment variables can be used. If the editor is closed without changes, None is returned. In case a file is edited directly the return value is always None and save and ext are ignored.

Takes:

  • editor: accepts default to get the default platform editor. But you can also provide the path to an editor e.g vi.

  • ext: the extension to tell the editor about. This defaults to .txt but changing this might change syntax highlighting e.g “.py”

  • save: accepts True or False to determine to save a file.

  • filename: accepts the path of a file you’d like to edit.

  • env: accepts any given environment variables to pass to the editor

Launches an instance of the users preferred editor on a temporary file. Once the user exits their editor, the contents of the temporary file are read in as the result. The editor to use is determined by reading the VISUAL or EDITOR environment variables. If neither of those are present, notepad (on Windows) or vim (Linux or Mac) is used.

Question Properties

A question is a dictionary containing question related values:

  • type: (String) Type of the prompt. Possible values: input, confirm, list, rawlist, expand, checkbox, password, editor

  • name: (String) The name to use when storing the answer in the answers hash.

  • message: (String|Function) The question to print. If defined as a function, the first parameter will be the current inquirer session answers.

  • default: (String|Number|Array|Function) Default value(s) to use if nothing is entered, or a function that returns the default value(s). If defined as a function, the first parameter will be the current inquirer session answers.

  • choices: (Array|Function) Choices array or a function returning a choices array. If defined as a function, the first parameter will be the current inquirer session answers. Array values can be simple strings, or objects containing a name (to display in list) and a value (to save in the answers hash) property. The choices array can also contain a Separator.

  • validate: (Function) Receive the user input and should return true if the value is valid, and an error message (String) otherwise. If false is returned, a default error message is provided.

  • filter: (Function) Receive the user input and return the filtered value to be used inside the program. The value returned will be added to the Answers hash.

  • when: (Function, Boolean) Receive the current user answers hash and should return true or false depending on whether or not this question should be asked. The value can also be a simple boolean.

Prompt Options

prompt(questions, answers=None, **options) takes the answers of a previous prompt and the following options:

  • style: the style to use.

  • raise_keyboard_interrupt: with True pressing Ctrl-C raises KeyboardInterrupt. By default a message is printed and prompt returns an empty dictionary.

  • keyboard_interrupt_msg: the message for Ctrl-C. Defaults to Cancelled by user.

User Interfaces and Styles

The colors are set by a style:

from PyInquirer import prompt, style_from_dict, Token

style = style_from_dict({
    Token.QuestionMark: '#E91E63 bold',
    Token.Selected: '#673AB7 bold',
    Token.Answer: '#2196f3 bold',
})

answers = prompt(questions, style=style)

Windows Platform

``PyInquirer`` is build on prompt_toolkit which is cross platform, and everything that you build on top should run fine on both Unix and Windows systems. On Windows, it uses a different event loop (WaitForMultipleObjects instead of select), and another input and output system. (Win32 APIs instead of pseudo-terminals and VT100.)

It’s worth noting that the implementation is a “best effort of what is possible”. Both Unix and Windows terminals have their limitations. But in general, the Unix experience will still be a little better.

For Windows, it’s recommended to use either cmder or conemu.

Support

Most of the questions are probably related to using a question type or feature. Please lookup and study the appropriate examples.

You can also open an issue on GitHub.

For many issues like for example common Python programming issues stackoverflow might be a good place to search for an answer.

Contribution

$ git clone git@github.com:CITGuru/PyInquirer.git
$ cd PyInquirer
$ python -m venv venv
$ source venv/bin/activate
$ pip install --upgrade pip
$ pip install -r requirements.txt
$ pip install -r requirements_dev.txt

With an environment ready you can add new feature and check everything works just fine

$ pytest -sv tests/

That’s it, now you can fork a project and submit PR with your change!

License

Copyright (c) 2018 Oyetoke Toby (twitter: @oyetokeT)

Licensed under the MIT license.

Metadata

Release files for PyInquirer 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyInquirer 1.0.4
File Size Uploaded Provenance Actions
pyinquirer-1.0.4.tar.gz 30.2 kB Without provenance Details for pyinquirer-1.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for PyInquirer 1.0.4
File Interpreter ABI Platform Actions
pyinquirer-1.0.4-py2.py3-none-any.whl Python 2, Python 3 none any Details for pyinquirer-1.0.4-py2.py3-none-any.whl

Total release size: 63.0 kB

Release files / pyinquirer-1.0.4.tar.gz

Download URL pyinquirer-1.0.4.tar.gz
Size 30.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e24bd990c48451f7ed91c34775ea584996bb712855baf4264ae018a95cb38bb1
BLAKE2b-256 checksum
How to use checksums
66aee6c6220f0950abe3d6d452f315239573e3b7877c6a0c841306ce21105629
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.4

Release files / pyinquirer-1.0.4-py2.py3-none-any.whl

Download URL pyinquirer-1.0.4-py2.py3-none-any.whl
Size 32.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
19520d1b6ea5ee7b8a036b8aa12d99033475652f5aaac2a0c514136a5fb304a3
BLAKE2b-256 checksum
How to use checksums
627e2076db1b7cef435710eb6602b2f840e53c8c654719d9012f77b59e85441d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.4

Security for PyInquirer 1.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page