Python Glossary Русский

all Python 3getpassfunction

getpass.getpass()

password input

Prompts for a password without echoing the typed characters and returns the string without the newline.

Syntax

password = getpass.getpass(prompt='Password: ', stream=None, *, echo_char=None)

The trap: input is read from the terminal (/dev/tty on Unix, the console on Windows), not from sys.stdin, so 'echo pw | python app.py' does not supply the password. In an IDE without a real terminal echo cannot be turned off: you get GetPassWarning and the password is visible. In tests and automated grading the function is replaced with unittest.mock.patch. echo_char='*' (3.14+) shows asterisks instead of nothing.

Examples

import getpass
from unittest import mock
with mock.patch('getpass.getpass', return_value='s3cret'):
    password = getpass.getpass('Пароль: ')  # настоящий вызов ждал бы ввода с терминала
print(len(password))  # → 6
# Python 3.14+
import getpass, inspect
print('echo_char' in inspect.signature(getpass.getpass).parameters)  # → True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy