7

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 Python 2.7 on Canopy.

import sys
import getpass

p = getpass.getpass()
print p
jlcv
  • 1,688
  • 5
  • 21
  • 50

6 Answers6

7

It is correct that Python "effectively freezes because it can't receive the input from standard input", however, for windows you can prefix your command with winpty. Then password can be inputted correctly when started like:

winpty python fileToExecute.py

winpty provides a interface similar to a Unix pty-master in a way that communication is also possible from windows terminals.

edi
  • 917
  • 7
  • 17
6

Python "effectively freezes because it can't receive the input from standard input". See https://support.enthought.com/entries/22157050-Canopy-Python-prompt-QtConsole-Can-t-run-getpass-or-interactive-OS-shell-commands-or-Windows-process

The fix is to use a different interpreter. I switched to IDLE and fixed the issue.

jlcv
  • 1,688
  • 5
  • 21
  • 50
2

Faced the same issue with getpass (mingw64) and found this simple solution.

os.system("stty -echo")
password = input('Enter Password:')
os.system("stty echo")
print("")
Shtefan
  • 742
  • 12
  • 14
1

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()
    # ...
culix
  • 10,188
  • 6
  • 36
  • 52
  • I just tested this - it does not work in all cases. Ex: On PyCharm in Windows hitting run would cause the terminal to hang however if I debugged it worked. Using this fix it rejects both instances. – Grant Curell Mar 01 '21 at 15:32
  • It did work for all the other use cases in which I tried it. So I'm still using it though! – Grant Curell Mar 01 '21 at 15:49
  • @GrantCurell Thanks for the feedback. Do you have a suggestion for a fix or alternate code that would work with PyCharm? Any idea why PyCharm hangs? – culix Mar 01 '21 at 16:45
  • 1
    I honestly don't - I'm not actually sure why it's doing it. I played with it for a bit, but honestly it only happened in PyCharm. I'm not sure what the difference is with its debug terminal, but on the regular Windows command line I had no problems which is what the people using my code are going to see anyway so I left it. – Grant Curell Mar 01 '21 at 18:08
0

I also had this on mac with both Jupyter Lab and Jupyter Notebook. For me the issue was caused by the variable name.

Naming the variable PG_REMOTEPASSWORD caused a hang but PG_PASSWORD & PG_ABCPass didn't. I don't know why that is an issue, there's nothing in docs about restrictions on what a variable can be called.

My setup up is Anaconda running Python 3.7.7

mapping dom
  • 1,737
  • 4
  • 27
  • 50
0

In pycharm RUN i had similar issue. When i opened files using Terminal "CMD" issue resolved.