Python getpass.getpass() function call hangs

后端 未结 5 925
广开言路
广开言路 2021-01-06 01:10

I am trying to get a prompt that will ask for my password but when I try to call getpass.getpass() it just freezes. I am running on Windows 7 64 bit using Pytho

5条回答
  •  囚心锁ツ
    2021-01-06 01:35

    getpass() will freeze if python is unable to read properly from standard input. This can happen on e.g. some Windows terminals, such as using git bash. You can use the sys module to detect if this will happen, to avoid hanging:

    import getpass
    import sys
    
    # ...
    
    if not sys.stdin.isatty():
        # notify user that they have a bad terminal
        # perhaps if os.name == 'nt': , prompt them to use winpty?
        return
    else:
        password = getpass.getpass()
        # ...
    

提交回复
热议问题