Hide input password when typing in Python

Viewed 4524

I need to write a code for an input password, like when I need to type the password it needs to layer the password like this *****. Notice that to code I'm using PyCharm.

enter image description here

2 Answers

There is no easy way to do this. However, you can use a couple of modules to create a similar function.

  1. PYAUTOGUI
import pyautogui

paswrd = pyautogui.password(text='', title='', default='', mask='*')

This opens a small window in which you can enter your password. Here, the password will be displayed as you want: it will use *s.

  1. GETPASS
import getpass

paswrd = getpass.getpass()

This will allow the user to enter a password normally, and the password won't show. It will be hidden. THIS ONLY WORKS IN THE TERMINAL, NOT SHELL

You can use the getpass module to hide your password:

from getpass import getpass

password = getpass(prompt='Input your password: ') # the default prompt is 'Password: '

This won't replace it with * but at least you can't see the password.

Note: It will not work in the shell. Thanks to @PilotDude

Related