I'm trying to use getpass to hide the input but it just gives me this error:
"Warning: QtConsole does not support password mode, the text you type will be visible."
I'm using Spyder. Here is my code:
import getpass
pswd = getpass.getpass('Password:')
if pswd== 'whatever':
print ('\nACCESS GRANTED')
else:
print('\nACCESS DENITED')
According to a comment by Carlos Cordoba (a developer for Spyder) on a duplicate but officially unanswered question, the warning you are receiving is a limitation of Spyder/QtConsole, not getpass
. He suggests using an external terminal in Spyder:
[There is no workaround] that will run inside Spyder. You can go to
Run > Configuration per file > Console
and select the option calledExecute in an external terminal
to use an external terminal instead.
I went ahead and wrote the small snippet below based on @Carlos Cordoba comment that since Spyder is based in PyQt, you have that package for sure :
def prompt_password(user):
"""
Parameters
----------
user : user name
Returns
-------
text : user input password
"""
from PyQt5.QtWidgets import QInputDialog, QLineEdit, QApplication
from PyQt5.QtCore import QCoreApplication
# Let's avoid to crash Qt-based dev environment (Spyder...)
app = QCoreApplication.instance()
if app is None:
app = QApplication([])
text, ok = QInputDialog.getText(
None,
"Credential",
"user {}:".format(user),
QLineEdit.Password)
if ok and text:
return text
raise ValueError("Must specify a valid password")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With