blob: 82b11919a3d2bf67eeb7ef40e80d51b93f96a4bb [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`getpass` --- Portable password input
2==========================================
3
4.. module:: getpass
5 :synopsis: Portable reading of passwords and retrieval of the userid.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Piers Lauder <piers@cs.su.oz.au>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00009.. Windows (& Mac?) support by Guido van Rossum.
Georg Brandl116aa622007-08-15 14:28:22 +000010
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040011**Source code:** :source:`Lib/getpass.py`
12
13--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015The :mod:`getpass` module provides two functions:
16
17
Georg Brandl036490d2009-05-17 13:00:36 +000018.. function:: getpass(prompt='Password: ', stream=None)
Georg Brandl116aa622007-08-15 14:28:22 +000019
Georg Brandle6bcc912008-05-12 18:05:20 +000020 Prompt the user for a password without echoing. The user is prompted using
R David Murray604453c2014-04-14 10:28:58 -040021 the string *prompt*, which defaults to ``'Password: '``. On Unix, the
22 prompt is written to the file-like object *stream* using the replace error
23 handler if needed. *stream* defaults to the controlling terminal
24 (:file:`/dev/tty`) or if that is unavailable to ``sys.stderr`` (this
25 argument is ignored on Windows).
Christian Heimes81ee3ef2008-05-04 22:42:01 +000026
27 If echo free input is unavailable getpass() falls back to printing
28 a warning message to *stream* and reading from ``sys.stdin`` and
29 issuing a :exc:`GetPassWarning`.
Georg Brandl116aa622007-08-15 14:28:22 +000030
Christian Heimes81ee3ef2008-05-04 22:42:01 +000031 .. note::
32 If you call getpass from within IDLE, the input may be done in the
33 terminal you launched IDLE from rather than the idle window itself.
34
35.. exception:: GetPassWarning
36
37 A :exc:`UserWarning` subclass issued when password input may be echoed.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039
40.. function:: getuser()
41
Berker Peksag1b207c52016-06-01 18:26:18 -070042 Return the "login name" of the user.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 This function checks the environment variables :envvar:`LOGNAME`,
Barry Warsawd4990312018-01-24 12:51:29 -050045 :envvar:`USER`, :envvar:`LNAME` and :envvar:`USERNAME`, in order, and
46 returns the value of the first one which is set to a non-empty string. If
47 none are set, the login name from the password database is returned on
48 systems which support the :mod:`pwd` module, otherwise, an exception is
49 raised.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Barry Warsawd4990312018-01-24 12:51:29 -050051 In general, this function should be preferred over :func:`os.getlogin()`.