Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getpass is not working for spyder (Python)

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')
like image 261
Ahmed Sameh Wagih Avatar asked Nov 09 '17 23:11

Ahmed Sameh Wagih


2 Answers

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 called Execute in an external terminal to use an external terminal instead.

like image 81
HFBrowning Avatar answered Oct 13 '22 01:10

HFBrowning


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")
like image 20
GBy Avatar answered Oct 13 '22 01:10

GBy