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.
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.
Like most Python packages PyInquirer is available on PyPI. Simply use pip to install the PyInquirer package
pip install PyInquirerPyInquirer requires Python 3.8 or newer and prompt_toolkit 3.
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, by the
nameof the questions
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 appA good starting point from here is probably the examples section.
Most of the examples intend to demonstrate a single question type or feature:
- editor.py
- expand.py
- list.py
- password.py
- when.py
- checkbox.py
- confirm.py
- hierarchical.py
- pizza.py - demonstrate using different question types
- input.py
- rawlist.py
To launch the examples from a clone of the repository, install the package and run them from the project directory:
pip install -e .
PYTHONPATH=. python examples/list.pyquestions is a list of questions. Each question has a type.
Take type, name, message, choices[, default, filter] properties. (Note that default must be the choice index in the array or a choice value)
Take type, name, message, choices[, default, filter] properties. (Note that default must be the choice index in the array or a choice value). At most 9 choices are supported.
Take type, name, message, choices[, default] properties. (Note that default must be the key or the index of a choice. If default key not provided, then help will be used as default choice)
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.
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, otherwise it'll default to "Disabled". The disabled property can also be a synchronous function receiving the current answers as argument and returning a boolean or a string.
The validate function receives the list of the selected values.
The pointer_index kwarg can be used to specify initial pointer position.
Take type, name, message[, default] properties. default is expected to be a boolean if used.
Take type, name, message[, default, filter, validate] properties.
Take type, name, message[, default, filter, validate] properties.
Take type, name, message[, default, filter, validate, eargs] properties
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. The answer is the text as of closing the editor, without the final newline unless default ends with one. If validate rejects it, the error is shown and the editor can be opened again. With filename the file is edited directly and its content is validated, the answer is the filename and save and ext are ignored. Without editor the text is entered in a multiline prompt, finish with escape and enter.
Takes:
- editor: accepts
defaultto get the default platform editor. But you can also provide the path to an editor e.gvi. - ext: the extension to tell the editor about. This defaults to
.txtbut changing this might change syntax highlighting e.g ".py" - save: with
Truethe edited text is only used if it has been saved. - 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 the first of vim, nano and vi (Linux or Mac) is used.
A question is a dictionary containing question related values:
type: (String) Type of the prompt. Possible values: input, confirm, list, rawlist, expand, checkbox, password, editorname: (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), a value (to save in the answers hash) and a short (to display after selection) properties. The choices array can also contain a Separator.validate: (Function, Validator) 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. All types with text input also accept aValidatorof prompt_toolkit.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.pageSize: (Number) Change the number of lines that will be rendered when using list or checkbox.mouse_support: (Boolean) Set toFalseto ignore the mouse in list, rawlist, expand and checkbox questions. Defaults toTrue.qmark: (String) The text in front of the message. Defaults to?.
prompt(questions, answers=None, **options) takes the answers of a previous prompt and the following options:
style: the style to use, see below.raise_keyboard_interrupt: withTruepressing Ctrl-C raisesKeyboardInterrupt. By default a message is printed andpromptreturns an empty dictionary.keyboard_interrupt_msg: the message for Ctrl-C. Defaults toCancelled by user.patch_stdout: withTrueoutput of other threads is printed above the prompt.
The colors are set by a style, which maps the parts of a prompt to prompt_toolkit styles:
from PyInquirer import prompt, style_from_dict
style = style_from_dict({
'questionmark': '#FF9D00 bold',
'question': 'bold',
'instruction': '',
'pointer': '#FF9D00 bold',
'selected': '#5F819D',
'separator': '#6C6C6C',
'answer': '#5F819D bold',
})
answers = prompt(questions, style=style)PyInquirer is built 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.
Most of the questions are probably related to using a question type or feature. Please look up and study the appropriate examples first.
For bugs and feature requests open an issue on GitHub. For common Python programming issues Stack Overflow might be a good place to search for an answer.
git clone git@github.com:CITGuru/PyInquirer.git
cd PyInquirer
python -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -e .
pip install pytest ptyprocessWith an environment ready you can add a new feature and check that everything works just fine
python -m pytest tests/That's it, now you can fork a project and submit PR with your change!
This project exists thanks to all the people who contribute!
Thank you to all our backers! Become a backer.
Support us by becoming a sponsor. Your logo will show up here with a link to your website. Become a sponsor.
Copyright (c) 2018 Oyetoke Toby (twitter: @oyetokeT)
Licensed under the MIT license.







