[Tutor] New to Python - print function - invalid syntax

Marc Tompkins marc.tompkins at gmail.com
Tue Aug 5 16:56:21 CEST 2014


On Tue, Aug 5, 2014 at 12:48 AM, Maxime Steisel <maximesteisel at gmail.com> wrote:
> I think this is because on windows, *.py files are associated with py.exe
> that choose the python version depending on the first line of your file.

No.  *ix operating systems (Unix, Linux, OS X, etc.) inspect the first
line of a file to determine how to handle it; Windows does NOT.
Windows simply looks at the filename extension (.py, .pyw, etc.) and
consults its internal registry of file type associations.  The way
that you, as a user, can edit those associations has changed over the
years; in Windows 7 and 8, it's Control Panel\All Control Panel
Items\Default Programs\Set Associations.

On the other hand, when you start up CMD.EXE and type "python" at the
prompt, Windows uses a procedure that goes back to DOS 2 or so: the
PATH environment variable.  This is similar to, but a little different
from, the way that *ixes work; Windows first compares what you've
typed with the list of commands built-in to CMD, then with all of the
executable files in the current working directory, THEN
walks through the directories listed in PATH.  The first matching
command or executable it finds is the one that runs.

> Putting #!python3 on the first line of game_over2.py should solve it.
NO.  #! has no effect in Windows, because the choice of Python
interpreter has already been made by the time anybody gets round to
reading the first line of the file.


More information about the Tutor mailing list