Two Questions about Python on Windows

W

Walter Hurry

Normally my Python development is done on FreeBSD and Linux. I know that on *ix I simply have to make foo.py executable (the shebang line is present, of course) to make it runnable.

For my son's school assignment, I have to help him with Python for Windows.

As I understand it, on Windows a .py file is not executable, so I need to run 'python foo py', or use a .pyw file.

Question 1: Do I make a .pyw file simply by copying or renaming foo.py to foo.pyw?

Secondly, on *ix, if there's an up-to-date .pyc in the right place and I run foo.py, Python will automagically use foo.pyc.

Question 2: Does it work the same way on Windows, and does this apply both to foo.py and foo.pyw?
 
M

maxerickson

Normally my Python development is done on FreeBSD and Linux. I know that on *ix I simply have to make foo.py executable (the shebang line is present, of course) to make it runnable.

For my son's school assignment, I have to help him with Python for Windows.

As I understand it, on Windows a .py file is not executable, so I need torun 'python foo py', or use a .pyw file.

Question 1: Do I make a .pyw file simply by copying or renaming foo.py tofoo.pyw?

Secondly, on *ix, if there's an up-to-date .pyc in the right place and I run foo.py, Python will automagically use foo.pyc.

Question 2: Does it work the same way on Windows, and does this apply both to >foo.py and foo.pyw?

The compile caching is more or less the same. It works transparently.

If you are working in a cmd shell, it doesn't matter what extension you use, pyw just allows for associating scripts with an interpreter stub that does not bring up a shell (for a gui or whatever). Console and gui programs are handled separately on Windows and that's how Python makes the option available to scripts.

You can also edit the PATHEXT environment variable to include .py/.pyw, making the python source files executable (as long as the types are properly registered with Windows; if double clicking runs them they should be properly registered).


Max
 
I

Ian Kelly

Normally my Python development is done on FreeBSD and Linux. I know that
on *ix I simply have to make foo.py executable (the shebang line is
present, of course) to make it runnable.
For my son's school assignment, I have to help him with Python for Windows.

As I understand it, on Windows a .py file is not executable, so I need to
run 'python foo py', or use a .pyw file.
Question 1: Do I make a .pyw file simply by copying or renaming foo.py to
foo.pyw?

Yes. The only distinction between .py and .pyw is that the Python installer
associates the former with Python.exe and the latter with Pythonw.exe.
Pythonw runs the script without creating a console window for stdin/stdout.
Secondly, on *ix, if there's an up-to-date .pyc in the right place and I
run foo.py, Python will automagically use foo.pyc.

I don't believe this is exactly correct. Python will only use a .pyc
automatically for imported modules. For a file specified on the command
line, Python won't try to second-guess the user as to which file they
want. The file could have a .php extension, and Python will happily run it
if the contents are valid Python code.
Question 2: Does it work the same way on Windows, and does this apply
both to foo.py and foo.pyw?

Yes.
 
E

Ethan Furman

I don't believe this is exactly correct. Python will only use a .pyc automatically for imported modules. For a file
specified on the command line, Python won't try to second-guess the user as to which file they want. The file could
have a .php extension, and Python will happily run it if the contents are valid Python code.

Right. When specifying the file as a command-line argument to python, you have it include the extension, and that is
exactly the file that python executes. Also, using this method, a .pyc file is not created. Only imported files may
have a .py[co] file automatically created for corresponding .py file.
 
T

Terry Reedy

Normally my Python development is done on FreeBSD and Linux. I know that on *ix I simply have to make foo.py executable (the shebang line is present, of course) to make it runnable.

For my son's school assignment, I have to help him with Python for Windows.

As I understand it, on Windows a .py file is not executable, so I need to run 'python foo py', or use a .pyw file.

If he edits the file with Idle (see Start menu Python directory, F5 runs
the file and uses the Idle shell for input and output. When the program
finishes, one can interactively examine objects and call functions. F5
Run is like running a program from a console with -I.
 
M

Mark Lawrence

that on *ix I simply have to make foo.py executable (the shebang line is
present, of course) to make it runnable.
need to run 'python foo py', or use a .pyw file.
foo.py to foo.pyw?

Yes. The only distinction between .py and .pyw is that the Python
installer associates the former with Python.exe and the latter with
Pythonw.exe. Pythonw runs the script without creating a console window
for stdin/stdout.

Not with more modern versions of Python.

c:\Users\Mark>assoc .py
..py=Python.File

c:\Users\Mark>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*

c:\Users\Mark>assoc .pyw
..pyw=Python.NoConFile

c:\Users\Mark>ftype Python.NoConFile
Python.NoConFile="C:\Windows\pyw.exe" "%1" %*
 
M

Mark Lawrence

Which ultimately calls some version of python.exe.


Which ultimately calls some version of pythonw.exe.

I elided that detail because it wasn't relevant to the question at hand.

Yes, I regretted how badly I worded the above the second I hit the send
button.
 
T

Tim Roberts

You can also edit the PATHEXT environment variable to include .py/.pyw,
making the python source files executable (as long as the types are
properly registered with Windows; if double clicking runs them they
should be properly registered).

Let me clarify that just a bit.

There are several ways to run Python scripts from a Windows command line.
You can invoke the interpreter specifically by name:
C:\Apps\Python27\python xxx.py
C:\Apps\Python27\python xxx.pyw

Or, if the Python directory is in your path:
python xxx.py
python xxx.pyw

If the .py and .pyw extension are registered using assoc and ftype (which
the Windows installer does), then you can invoke them by file name:
xxx.py
xxx.pyw

If you add those extensions to PATHEXT, then you do not even need to
provide the extension, so they will look just like any other automatically
executable program:
xxx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top