I have a work around for you, which you can modify to your needs:
- Install getch:
pip install getch
. This module has methods for getting input char by char.
Create a function that gets user input char by char and prints *
in it's place:
#import sys (if option 1. is used)
import getch
def get_password():
p = ''
typed = ''
while True:
typed = getch.getch()
if typed == '\n':
print(typed)
break
p += typed
# Choose one of the following solutions:
# 1. General way. Needs import sys
sys.stdout.write('*')
sys.stdout.flush()
# 2. Python 3 way:
print('*', end='', flush=True)
return p
Good luck :)
EDIT: For @timgeb 's comment about security:
From getpass documentation:
On Unix, the prompt is written to the file-like object stream using the replace error handler if needed.
stream defaults to the controlling terminal (/dev/tty
) or if that is unavailable to sys.stderr
(this argument is ignored on Windows).
So it behaves quite similarly with the function above, except that mine has no fallback option...