ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](curses.xhtml "curses --- 终端字符单元显示的处理") | - [上一页](logging.handlers.xhtml "logging.handlers --- Logging handlers") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [通用操作系统服务](allos.xhtml) » - $('.inline-search').show(0); | # [`getpass`](#module-getpass "getpass: Portable reading of passwords and retrieval of the userid.") --- 便携式密码输入工具 **源代码:** [Lib/getpass.py](https://github.com/python/cpython/tree/3.7/Lib/getpass.py) \[https://github.com/python/cpython/tree/3.7/Lib/getpass.py\] - - - - - - [`getpass`](#module-getpass "getpass: Portable reading of passwords and retrieval of the userid.") 模块提供了两个函数: `getpass.``getpass`(*prompt='Password: '*, *stream=None*)提示用户输入一个密码且不会回显。 用户会看到字符串 *prompt* 作为提示,其默认值为 `'Password: '`。 在 Unix 上,如有必要提示会使用替换错误句柄写入到文件类对象 *stream*。 *stream* 默认指向控制终端 (`/dev/tty`),如果不可用则指向 `sys.stderr` (此参数在 Windows 上会被忽略)。 如果回显自由输入不可用则 getpass() 将回退为打印一条警告消息到 *stream* 并且从 `sys.stdin` 读取同时发出 [`GetPassWarning`](#getpass.GetPassWarning "getpass.GetPassWarning")。 注解 如果你从 IDLE 内部调用 getpass,输入可能是在你启动 IDLE 的终端中而非在 IDEL 窗口本身中完成。 *exception* `getpass.``GetPassWarning`一个当密码输入可能被回显时发出的 [`UserWarning`](exceptions.xhtml#UserWarning "UserWarning") 子类。 `getpass.``getuser`()返回用户的“登录名称”。 此函数会按顺序检查环境变量 `LOGNAME`, `USER`, `LNAME` 和 `USERNAME`,并返回其中第一个被设置为非空字符串的值。 如果均未设置,则在支持 [`pwd`](pwd.xhtml#module-pwd "pwd: The password database (getpwnam() and friends). (Unix)") 模块的系统上将返回来自密码数据库的登录名,否则将引发一个异常。 通常情况下,此函数应优先于 [`os.getlogin()`](os.xhtml#os.getlogin "os.getlogin") 使用。 ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](curses.xhtml "curses --- 终端字符单元显示的处理") | - [上一页](logging.handlers.xhtml "logging.handlers --- Logging handlers") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [通用操作系统服务](allos.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。