blob: e735bfe35470b7b1618e203be9cd777022c8d658 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`getpass` --- Portable password input
2==========================================
3
4.. module:: getpass
5 :synopsis: Portable reading of passwords and retrieval of the userid.
6.. moduleauthor:: Piers Lauder <piers@cs.su.oz.au>
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Georg Brandlb19be572007-12-29 10:57:00 +00008.. Windows (& Mac?) support by Guido van Rossum.
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
10The :mod:`getpass` module provides two functions:
11
12
13.. function:: getpass([prompt[, stream]])
14
15 Prompt the user for a password without echoing. The user is prompted using the
16 string *prompt*, which defaults to ``'Password: '``. On Unix, the prompt is
Gregory P. Smith19b44112008-04-22 08:08:41 +000017 written to the file-like object *stream*. *stream* defaults to the
18 controlling terminal (/dev/tty) or if that is unavailable to ``sys.stderr``
19 (this argument is ignored on Windows).
20
21 If echo free input is unavailable getpass() falls back to printing
22 a warning message to *stream* and reading from ``sys.stdin`` and
23 issuing a :exc:`GetPassWarning`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000024
Georg Brandl8ec7f652007-08-15 14:28:01 +000025 .. versionchanged:: 2.5
26 The *stream* parameter was added.
Gregory P. Smith19b44112008-04-22 08:08:41 +000027 .. versionchanged:: 2.6
28 On Unix it defaults to using /dev/tty before falling back
29 to ``sys.stdin`` and ``sys.stderr``.
Gregory P. Smith6c6f4f72008-04-23 01:06:42 +000030 .. note::
31 If you call getpass from within IDLE, the input may be done in the
32 terminal you launched IDLE from rather than the idle window itself.
Gregory P. Smith19b44112008-04-22 08:08:41 +000033
34
35.. exception:: GetPassWarning
36
37 A :exc:`UserWarning` subclass issued when password input may be echoed.
Georg Brandl8ec7f652007-08-15 14:28:01 +000038
39
40.. function:: getuser()
41
Berker Peksag56da8fa2016-06-01 18:32:42 -070042 Return the "login name" of the user.
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
44 This function checks the environment variables :envvar:`LOGNAME`,
45 :envvar:`USER`, :envvar:`LNAME` and :envvar:`USERNAME`, in order, and returns
46 the value of the first one which is set to a non-empty string. If none are set,
47 the login name from the password database is returned on systems which support
48 the :mod:`pwd` module, otherwise, an exception is raised.
49