9

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')
HFBrowning
  • 2,196
  • 3
  • 23
  • 42

3 Answers3

5

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.

HFBrowning
  • 2,196
  • 3
  • 23
  • 42
4

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")
GBy
  • 1,719
  • 13
  • 19
  • I'm not quite as familiar with the underlying code here as others in this post, but I'm having trouble seeing how this solves the problem OP raises -- is this function to be used in lieu of `getpass.getpass()`? This function only seems to prompt the user if one enters their actual system username in the function. In a case where you might be sharing code or a script across different users, would it be the case that you would have to prompt for `user` specifically each time so that it matches their specific username? – econometrica_33 Oct 29 '21 at 15:27
0

I'd love to see a better answer, accessible directly from the API or from editor magic, but I have seen this open for a long time. In the meantime I have two quick-and-dirty workarounds:

  1. More general -- Use your system's basic GUI toolkit with subprocess. osascript is the Mac version. Please comment/add the equivalent Windows and Linux hacks:

    # great reference: https://scriptingosx.com/2018/08/user-interaction-from-bash-scripts/
    import subprocess
    subprocess.getoutput("""osascript -e 'display dialog "Who are you?" default answer "nobody" with hidden answer'""")
    
  2. Quicker, using your editor's variable explorer -- Declare a temp variable, edit it in the "Variable Explorer", assign it to your desired variable, then del it:

    temp = ''
    # now go to the Variable explorer window and change the value of foo to 'My secret'
    PASSWORD = temp # Spyder's Variable Explorer by default omits names that are all-uppercase 
    del temp
    
Leo
  • 2,775
  • 27
  • 29
  • I think your first solution is pretty good and can be generalized a bit more: you could write a small PyQt app with a QLineEdit that hides its text, then capture that text and save it in a variable. Since Spyder is based in PyQt, you have that package for sure, and the solution will be cross-platform, unlike your solution. – Carlos Cordoba Mar 30 '20 at 16:35
  • @CarlosCordoba See my proposed answer based on your comment. – GBy Jan 02 '21 at 11:17