0

I am working on a very simple python program that is basically a password entry screen. Everything is working as it should and the program runs correctly when I execute through the command prompt on Windows (python login.py). However, if I run the program by double clicking it, it takes me as far as the "Enter password: " input and as soon as I hit enter with my desired password, it gives this error:

NameError: name 'pass' is not defined.

How do I go about fixing this error?

import time

print("You have ten seconds to enter the password.")
time.sleep(1)
print("Or this computer will shut down.")
password = input("Enter password: ")

while 1:
    if password == "pass":
        print("Login successful.")
        break
    else:
        password = input("Enter password: ")
2
  • 1
    I suspect you're using Python 2.x in one instance and 3.x in the other - input in Python 2 is eval(raw_input(...)), so is trying to resolve pass as an identifier, not a string. Commented May 27, 2015 at 13:09
  • Odd, I uninstalled Python 2.7 (I had both 2 and 3 installed) and that appears to have fixed it. Commented May 27, 2015 at 13:14

1 Answer 1

1

Sounds like you're running Python 3.X from the command line, but Python 2.7 is executing your script when you click on it. The error occurs because 2.7's input behaves differently from 3.X's.

You could try to redefine input so it behaves the same in both versions:

try:
    input = raw_input
except NameError:
    #we're already in 3.x and don't need to do anything
    pass

#rest of code goes here

Alternatively, you could change the default application for .py files. How this is done probably varies from OS to OS, but in Windows it's something like: right click the .py file, choose "open with..." and "choose default program", then browse to the python executable in your 3.X directory.

Sign up to request clarification or add additional context in comments.

2 Comments

For Windows Python 3.3+, start an elevated command prompt to run ftype Python.File="path\to\py.exe" "%1" %*. Typically the launcher path is C:\Windows\py.exe. A script can ensure it runs using Python 3 by adding a virtual shebang line such as #!/usr/bin/python3. To instead make 2.x the system default -- without the shebang-aware py.exe launcher -- just use the path to python.exe.
The "open with" approach creates a broken ProgId that lacks %* for command-line arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.