2

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

3

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

3 Comments

Not clear to me what it means to not work in the shell. I'm running the getpass command with no issues in windows powershell. Same with command prompt. Does this break in bash or other shells?
@moosearch nope it works there too. One clue is - document says: If echo free input is unavailable getpass() falls back to printing a warning message to stream and reading from sys.stdin and issuing a GetPassWarning - I guess author's shell was one of those that can't take echo-free input somehow. I wonder how user login to such shell without leaking password in the first place though
@moosearch actually just found out that when running script in pycharm debug console it echos back. This might be the case
1

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

3 Comments

However, it should be noted that getpass doesn't work in the shell.
If using Spyder IDE, you'll get the following warning: QtConsole does not support password mode, the text you type will be visible.
@Ben I think it only works in the actual python console.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.