Python Forum
[split] running .py files on Windows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [split] running .py files on Windows (/thread-5024.html)



[split] running .py files on Windows - metulburr - Sep-14-2017

split from discussion https://python-forum.io/Thread-run-py-file-on-Windows

I dont mean to hijack but i kind of am doing it
Quote:From there, you can just type "hello.py".
You can just type the filename now in Windows? How do you define which python interpreter goes with which python file?


[split] running .py files on Windows - sparkz_alot - Sep-14-2017

The OP made no mention of multiple versions of Python, but if that is the case, there are two solutions, both of which follow the Linux methods, sort of.
1) Add a shebang line (which also aids when double clicking file)
2) Specify with the "python(version) hello.py"

If there is only one version, neither method one or method two is required.


RE: Can't run .py file - Windows 8.1 - nilamo - Sep-14-2017

(Sep-14-2017, 02:50 PM)metulburr Wrote: I dont mean to hijack but i kind of am doing it
Quote:From there, you can just type "hello.py".
You can just type the filename now in Windows? How do you define which python interpreter goes with which python file? 

File associations.  Windows doesn't care about shebang lines, but if .py is associated with python, then python will be used to "open" the file, which, for python, means it'll be run.  I don't think this works in the file explorer and double clicking the file (it's just opened in your editor for... editing).  There's a special python launcher (pythonw.exe) that ships along with python, which is what file associations are bound to.  THAT will (depending on the version, I think) check for a shebang line, and try to route the script to the right version of python if it's installed.


RE: Can't run .py file - Windows 8.1 - sparkz_alot - Sep-14-2017

(Sep-14-2017, 03:25 PM)nilamo Wrote: Windows doesn't care about shebang lines

Beginning with Python 3.4, Windows will honor the shebang line.

Quote:double clicking the file (it's just opened in your editor for... editing).

double clicking a .py or .pyw will 'run' the script, not open it.

Quote:There's a special python launcher (pythonw.exe) that ships along with python, which is what file associations are bound to.

The 'pythonw.exe' will suppress the command terminal from starting when running a 'GUI' script, such as tkinter. 'python.exe' will open a command terminal instance which will stay open as long as the script is running. This can prove useful if you want to see Tracebacks while the script is running.

Details can be found here: https://docs.python.org/3.4/using/windows.html


RE: Can't run .py file - Windows 8.1 - snippsat - Sep-14-2017

(Sep-14-2017, 03:25 PM)nilamo Wrote: I don't think this works in the file explorer and double clicking the file (it's just opened in your editor for... editing).
It work in explorer to,will look at same place in registry \Explorer\FileExts\.py

I think is good to be used like this python hello.py.
Have several version installed use py -version,to mange all version.
C:\1                                                                         
λ py -2.7 version.py                                                         
Hello from 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)]

C:\1
λ py -3.4 version.py
Hello from 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)]

# Main version in Path always python
C:\1
λ python version.py
Hello from 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]
I know that can use shebang(after 3.4) in windows,but i would not use.
It will just confuse people because almost no one use it.


RE: Can't run .py file - Windows 8.1 - nilamo - Sep-14-2017

(Sep-14-2017, 04:32 PM)sparkz_alot Wrote:
(Sep-14-2017, 03:25 PM)nilamo Wrote: Windows doesn't care about shebang lines

Beginning with Python 3.4, Windows will honor the shebang line.

Well, python will honor the shebang, and run a different version of python if it needs to. Updating python to 3.4 won't change how windows works.


RE: [split] running .py files on Windows - metulburr - Sep-14-2017

split - merging


RE: [split] running .py files on Windows - sparkz_alot - Sep-14-2017

(Sep-14-2017, 04:42 PM)snippsat Wrote: I know that can use shebang(after 3.4) in windows,but i would not use. It will just confuse people because almost no one use it.

lol, I'm one of those that uses it all the time, even prior to version 3.4. Since I have 3 Linux machines (1 with 2.7 and 3.4 and 2 with 2.7, 3.4 and 3.6.2) I've just gotten in the habit of using it all the time.
Quote:Updating python to 3.4 won't change how windows works.

I don't think that even Microsoft has control any more as to how Windows works. Windows has become self-aware and now writes it's own code.  Shocked
What it does show, is that the Python gods have gone the extra mile towards making Python truly cross-platform, along with the utf-8 and Windows debacle, which Python resolved with 3.6.2 (though IMHO, it should have been MS who drops its antiquated code pages and switches to utf-8).