user286508
user286508

Reputation: 29

Python program works from command prompt but does not work when opened in Windows

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: ")

Upvotes: 0

Views: 1432

Answers (1)

Kevin
Kevin

Reputation: 76184

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.

Upvotes: 1

Related Questions