0

I am using Python. I am making a script where the user has to enter the password in the terminal.

I have already found a solution on this website by using the getpass module.

new_password=getpass.getpass(prompt="Type new password: ")

The problem is I get a warning and the password input gets displayed as well.

Warning (from warnings module):
  File "C:\Python34\lib\getpass.py", line 101
    return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Borut Flis
  • 15,715
  • 30
  • 92
  • 119
  • `getpass.win_getpass()` for widows use this. – Ja8zyjits Dec 15 '16 at 09:37
  • unfortunately doesn't help – Borut Flis Dec 15 '16 at 09:46
  • are you using IDLE or windows terminal? – Ja8zyjits Dec 15 '16 at 10:20
  • I am using IDLE – Borut Flis Dec 16 '16 at 10:27
  • IDLE has issue with the stdout and stderr, try to use the normal terminal [question-1](http://stackoverflow.com/questions/38878741/getpasswarning-can-not-control-echo-on-the-terminal-when-running-from-idle); [question-2](http://stackoverflow.com/questions/21264701/getpass-getpass-error-without-other-code) – Ja8zyjits Dec 16 '16 at 14:39
  • Possible duplicate of ["GetPassWarning: Can not control echo on the terminal" when running from IDLE](https://stackoverflow.com/questions/38878741/getpasswarning-can-not-control-echo-on-the-terminal-when-running-from-idle) – Georgy Oct 23 '18 at 15:07

2 Answers2

1

Use command prompt as admin to run this program.

Reason is because system environment where stdin, stdout and stderr are connected to /dev/tty, or another PTY-compliant device.

The IDLE REPL does not meet this requirement.

And change new_password=getpass.getpass(prompt="Type new password: ") to new_password=getpass.getpass("Type new password: ") if you are using Windows OS or new_password=getpass.getpass("Type new password: ", None) for Linux distributions.

This would help you for sure:

import getpass
pw = getpass.getpass("Enter Your Password Here: ")
if pw == "password":
    print("You are Welcome...")
else:
    print("Sorry! You're are not allowed.")

As per Python documentation:

getpass.getpass([prompt[, stream]])

Prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to 'Password: '. On Unix, the prompt is written to the file-like object stream. stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows)

Changed in version 2.5: The stream parameter was added.

Note: If you call getpass from within IDLE, the input may be done in the terminal you launched IDLE from rather than the idle window itself.

bryan
  • 130
  • 1
  • 6
0

Using a normal terminal with this code:

import getpass
new_password=getpass.getpass(prompt="Type new password: ")
print(new_password)

Will work alright, but if we try the same with IDLE we'll get the error you've exposed in your question.

Now, if we look at the docs here you'll see this is intended, it says:

Note If you call getpass from within IDLE, the input may be done in the terminal you launched IDLE from rather than the idle window itself.

BPL
  • 9,632
  • 9
  • 59
  • 117