Search code examples
pythonclassoopinitializationgetpass

Using getpass() fails in the self.__init__() method but works fine when used functionally. Why is this?


I can't work out what I'm doing wrong here. I want to set up a new user if this class is instantiated and there is no user already pickled. My __init__ method currently looks like this:

class User:
    def __init__(self, *args, **kwargs):
        if os.path.isfile(f"{get_cwd()}/user.pickle"):
            self.load_user()
        else:
            self.email_address = input("What's your email address?: ")
            self.email_password = base64.b64encode(getpass("What's your email password?: ").encode("utf-8"))
            self.save_user()

However, when I instantiate the class, it doesn't get past asking for self.email_address. If I change the self.email_password to a simple input function it works. What am I doing wrong with using getpass() here?

I've tried making a separate function to receive / encode the password variable and calling that from within the __init__ method but it does the same thing.

If I make the whole script functional if works (but it's a little lengthy!)

Why am I having problems with getpass and a class-based approach??

EDIT: was trying to run through PyCharm terminal and the above is more or less irrelevant.


Solution

  • Thank you to @Alex Hall for reminding me that getpass doesn't work through the PyCharm terminal. Works perfectly fine in a normal terminal.