14

When I run this code:

import getpass

p = getpass.getpass(prompt='digite a senha\n')
if p == '12345':
    print('YO Paul')
else:
    print('BRHHH')
print('O seu input foi:', p) # p = seu input

I got this warning:

Warning (from warnings module):
   File "/usr/lib/python3.4/getpass.py", line 63
    passwd = fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal. Warning: Password input may be echoed.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Paul Sigonoso
  • 575
  • 2
  • 5
  • 18
  • 1
    That would be system and terminal-dependent: https://docs.python.org/2/library/getpass.html Prompt the user for a password without echoing. ... 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. – handle Aug 10 '16 at 16:21
  • 5
    I think the warning is telling you precisely that there is *not* a way (that Python knows or recognizes) to prevent the password from being echoed on the system where the message is emitted. – John Bollinger Aug 10 '16 at 16:21
  • 1
    I am using IDLE: Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux – Paul Sigonoso Aug 10 '16 at 16:23
  • 1
    `getpass` working: http://stackoverflow.com/questions/4616813/can-i-get-console-input-without-echo-in-python – handle Aug 10 '16 at 16:23
  • 1
    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. – handle Aug 10 '16 at 16:23
  • 5
    IDLE interferes with normal console input & output; `getpass` was not designed to be used in such environments. – PM 2Ring Aug 10 '16 at 16:25

4 Answers4

15

Use an actual terminal -- that is, an environment where stdin, stdout and stderr are connected to /dev/tty, or another PTY-compliant device.

The IDLE REPL does not meet this requirement.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
5

Run your code in terminal, instead of the IDE. you will see that there is no more warning there. To run your code, enter this command in terminal:

python3 your_program.py
Reyhaneh Torab
  • 360
  • 5
  • 9
2

Rather than deal with changing the current working directory in a terminal that has not started Python (which would mean you type something like python3 script.py--and it will fail unless the current working directory is already specified), start Python in your terminal and run this one-line command:

exec(open('C:\folder\script.py').read())

where you change the path string 'C:\folder\script.py' to match wherever your file is located on disk (the string does need to be specified with quotes).

brethvoice
  • 350
  • 1
  • 4
  • 14
  • This will get rid of the warning. That being said, it also does not provide *any* feedback on how many characters you have entered, how many backspaces you have pressed, etc. so if your password is long then: ```getpass.getpass(prompt='Securely enter your password (minimum of xx characters): ')``` is about as good as you are going to get using that package. – brethvoice Feb 26 '20 at 16:50
1

use cmd ie. command prompt and then run the file in it.

like:

python abc.py
Dinokor
  • 73
  • 7