Posts: 5,151
Threads: 396
Joined: Sep 2016
Sep-14-2017, 02:50 PM
(This post was last modified: Sep-14-2017, 08:55 PM by metulburr.)
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?
Recommended Tutorials:
Posts: 1,298
Threads: 38
Joined: Sep 2016
Sep-14-2017, 03:17 PM
(This post was last modified: Sep-14-2017, 08:49 PM by sparkz_alot.)
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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.
Posts: 1,298
Threads: 38
Joined: Sep 2016
(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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-14-2017, 04:42 PM
(This post was last modified: Sep-14-2017, 04:42 PM by snippsat.)
(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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Sep-14-2017, 08:57 PM
(This post was last modified: Sep-14-2017, 08:57 PM by metulburr.)
Recommended Tutorials:
Posts: 1,298
Threads: 38
Joined: Sep 2016
(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. 
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).
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
|