xx23x 2016-01-13 03:55 采纳率: 33.3%
浏览 6393

求高手解救啊!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 == '\r' or c == '\n':
print "if c == '\r' or c == '\n':"
break
if c == '\003':
print "if c == '\003':"
raise KeyboardInterrupt
if c == '\b':
print "if c == '\b':"
pw = pw[:-1]
else:
print "else:"
pw = pw + c
msvcrt.putch('\r')
msvcrt.putch('\n')
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:\Python27\lib\getpass.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中都是正常的。
谢谢啦!!

  • 写回答

5条回答 默认 最新

  • freelyYU 2020-07-22 09:26
    关注

    我的也是,在pycahrm中,直接运行.py不行,那里卡住,不出来提示语。后来,以debug xxx.py运行,就出来提示语了,不会卡住了

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办