从 IDLE 运行时“GetPassWarning:无法控制终端上的回显”
Posted
技术标签:
【中文标题】从 IDLE 运行时“GetPassWarning:无法控制终端上的回显”【英文标题】:"GetPassWarning: Can not control echo on the terminal" when running from IDLE 【发布时间】:2016-12-17 03:39:47 【问题描述】:当我运行这段代码时:
import getpass
p = getpass.getpass(prompt='digite a senha\n')
if p == '12345':
print('YO Paul')
else:
print('BRHHH')
print('O seu input foi:', p) # p = seu input
我收到了这个警告:
Warning (from warnings module):
File "/usr/lib/python3.4/getpass.py", line 63
passwd = fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal. Warning: Password input may be echoed.
【问题讨论】:
这将取决于系统和终端:docs.python.org/2/library/getpass.html 提示用户输入密码而不回显。 ... 如果无回显输入不可用,getpass() 将退回到打印警告消息以流式传输并从 sys.stdin 读取并发出 GetPassWarning。 我认为该警告准确地告诉您,不有一种方法(Python 知道或识别)来防止密码在消息所在的系统上回显发射。 我在 Linux 上使用 IDLE:Python 3.4.3(默认,2015 年 10 月 14 日,20:28:29)[GCC 4.8.4]getpass
工作:***.com/questions/4616813/…
IDLE 干扰正常的控制台输入和输出; getpass
并非设计用于此类环境。
【参考方案1】:
使用实际终端 -- 即 stdin
、stdout
和 stderr
连接到 /dev/tty
或其他 PTY 兼容设备的环境。
IDLE REPL 不满足此要求。
【讨论】:
【参考方案2】:在终端而不是 IDE 中运行您的代码。你会看到那里没有更多的警告。 要运行您的代码,请在终端中输入以下命令:
python3 your_program.py
【讨论】:
【参考方案3】:与其在尚未启动 Python 的终端中更改当前工作目录(这意味着您输入类似 python3 script.py
的内容,否则它将失败,除非已指定当前工作目录),而是在你的终端并运行这个单行命令:
exec(open('C:\folder\script.py').read())
更改路径字符串'C:\folder\script.py'
以匹配文件在磁盘上的任何位置(字符串确实 需要用引号指定)。
【讨论】:
这将消除警告。话虽如此,它也不提供关于您输入了多少个字符、按下了多少个退格键等的任何反馈,所以如果您的密码很长,那么:getpass.getpass(prompt='Securely enter your password (minimum of xx characters): ')
也差不多因为您将使用该软件包。【参考方案4】:
使用 cmd 即。命令提示符,然后在其中运行文件。
喜欢:
python abc.py
【讨论】:
以上是关于从 IDLE 运行时“GetPassWarning:无法控制终端上的回显”的主要内容,如果未能解决你的问题,请参考以下文章