pycharm下getpass.getpass()卡住

pycharm下getpass.getpass()卡住不运行是什么问题

首先声明 下面这几行代码在命令行和eclipse下都能正常运行

import getpass
print "nihao1"
d = getpass.getpass()
print "nihao2"
print "d=", d
运行程序只是打印nihao1,然后可以不停的输入,就是不继续往下运行了,程序一直卡在这里,我是windows系统

然后我查看getpass模块的源码,发现getpass.getpass()的调用的是getpass.win_getpass(), 在这个方法中有一行c=msvcrt,getch().程序就是卡在这里不动了。
我能找到的问题就到这了,,接下来也不知道该怎么解决 ,求大神啊!!!

PS:
刚才又研究了一下源码,win_getpass()方法源码:里面的print语句是我自己调的时候加上的。
def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
print "hahaha i am in "
if sys.stdin is not sys.__stdin__:
print "if sys.stdin is not sys.__stdin__:"
return fallback_getpass(prompt, stream)
import msvcrt
print "import msvcrt"
for c in prompt:
print "for c in prompt:",c
msvcrt.putch(c)
pw = ""
while 1:
print "while 1:"
c = msvcrt.getch()
print "is here "
print c
if c == ' ' or c == ' ':
print "if c == ' ' or c == ' ':"
break
if c == '03':
print "if c == '03':"
raise KeyboardInterrupt
if c == '':
print "if c == '':"
pw = pw[:-1]
else:
print "else:"
pw = pw + c
msvcrt.putch(' ')
msvcrt.putch(' ')
print "pw:", pw
return pw


第四行有一句 if sys.stdin is not sys.__stdin__:
在windows下 pycharm中,运行源码时这一句返回的是false,所以程序跳过if语句继续往下执行了,这就进入了while 1:循环, 然后就卡在了刚开始说的c=msvcrt.getch()这里了。我把上面的那个if语句改了一下,把is not 改成 is了,竟然可以运行了。只是多了这么几行输出:
D:Python27libgetpass.py:94: GetPassWarning: Can not control echo on the terminal.
return fallback_getpass(prompt, stream)
Warning: Password input may be echoed.

我只能做到这个程度了,至于为什么会是这样,为什么那个If语句在起作用,这和pycharm有什么关系,我都不知道了,所以求大神指点啊,说一下所以然,其中的原理是什么,应该怎么弄才能在pycharm中正常运行,因为源码在命令行和eclipse中都是正常的。
谢谢啦!!

原文地址:https://www.cnblogs.com/fmgao-technology/p/9036877.html