How to use getpass() to hide a users password as it is entered

Question:

I am trying to make a small program which checks the strength of the user password. When the user is prompted to enter their password I want the input to show as asterisks or dots instead of the text input. Is there a way of hiding this input.

I have used getpass to try and hide the input without success.

    from getpass import getpass
    passwd = getpass('Please enter your password')

This gives me the following warning message:

Warning (from warnings module):
File “C:Program Files (x86)Python36-32libgetpass.py”, line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Please enter your password: this_is_my_password

The password is entered on the line but instead of coming up with astrisks it comes up with the original characters.

As the program will check the strength of the password I need to have something that does not save the password as the asterisks but as its original characters.

As well as this is there any way of incorporating a way of showing the user password if they perform an action. I was considering using a button but that is an area in Python I am not confident with so could I program in another way with a specific key.

Sudo code for project.

userPass = hidden(input("Please enter your password "))
if showPassword selected:
    show the password
else:
    move on with the rest of the program
Asked By: Jake Symons

||

Answers:

The getpass module contains a getpass function that works just like input(), except that it turns the terminal echo off.

The result is the password entered, which you can use to determine its strength or print as needed, but while the user is entering their password, nothing is printed except the prompt.

>>> from getpass import getpass
>>> passwd = getpass('Please enter your password')
Please enter your password
>>> print passwd
This is my password
Answered By: 3Doubloons

The warning basically means you are running your program in an environment where Python cannot use the regular interactive IOCTL calls to implement the asterisks-only functionality. Running a program from inside your IDE basically causes the IDE to run the program in a pseudo-terminal. Try running it on the command line instead.

Answered By: tripleee
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.