[Tutor] Runs in IDLE with F5, but not in Windows Command Prompt

Steven D'Aprano steve at pearwood.info
Thu Apr 25 06:26:52 CEST 2013


On 25/04/13 14:11, boB Stepp wrote:
> In my on again, off again studies of Python I am stumped on something
> that ought to be trivial, but I am not seeing it. When I run  this
> program (only the part up to where the error occurs is shown):
>
> import random
>
> numberToGuess = random.randint(1, 20)
> numberOfGuesses = 0
>
> print("Hello! What is your name?")
> playerName = input()
>
> ...everything works fine from IDLE. However, in the Windows command prompt:
>
>
> E:\Programs\Python\IYOCGwPy\Ch4>python -V
> Python 3.3.1

Here you are explicitly calling "python". Windows searches the path for the first version of Python it can find, and finds Python 3.3.1.


> E:\Programs\Python\IYOCGwPy\Ch4>guess.py


Here you are telling Windows to look up the file association for .py files. It locates some program, and runs it with guess.py as the argument. Looking at the result:


> Hello! What is your name?
> boB
> Traceback (most recent call last):
>    File "E:\Programs\Python\IYOCGwPy\Ch4\guess.py", line 13, in <module>
>      playerName = input()
>    File "<string>", line 1, in <module>
> NameError: name 'boB' is not defined


this is obviously Python 2.x rather than 3.3.1. You can check that by putting these two lines at the very beginning of the program:

import sys
print(sys.version)


My bet is that if you run it like this:


python guess.py


it will print "3.3.1" and the program will work, but when you run it like this:


guess.py


it will print 2.something and then fail.



> E:\Programs\Python\IYOCGwPy\Ch4>
>
> This has me totally puzzled. I thought it might have something to do
> with the fact that I also have Python 2.7 installed, so I removed it
> from the path variable and also did the check for version that you see
> above.

There's your bunny. Just because you remove it from the search path doesn't mean that Windows won't use it.

You might like to remove Python 2.7 altogether, or at least unassociate it from the .py file extension.



-- 
Steven


More information about the Tutor mailing list