Python Forum

Full Version: [split] launch .PY program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(Sep-28-2016, 02:12 PM)metulburr Wrote: [ -> ]
Quote:in windows you can double click on a .py file to run it
You should avoid this method. Not only is it windows dependent, but its better to get into the habit of executing code from the terminal/command prompt (especially for a new programmer). Plus what if you have multiple versions of python installed and you want to test different versions with each error check?

When I started as an apprentice, I was told "Learn the trade, not the tricks of the trade."  I suppose that applies to programming as well. Darned if Python doesn't keep adding 'tricks' to the Windows version though :P .

An example is, starting with v3.3, windows now recognizes the shebang line, so now
#! /usr/bin/env python3
works in both Linux and Windows.

Another example is instead of invoking: python script.py, you only need: py script.py, or py -2 script.py, etc. I haven't tried it in Linux to see if this also works.

Darn you Python developers :D
(Sep-28-2016, 02:55 PM)sparkz_alot Wrote: [ -> ]An example is, starting with v3.3, windows now recognizes the shebang line, so now
Python Code: (Select All)
#! /usr/bin/env python3 

works in both Linux and Windows.

I have never heard of this. How exactly would windows know where to find python as the shebang line path is linux binaries directory?
If .py is associated with python in the file associations, AND the script is executable, then Windows just looks up the file association. It still ignores the shebang as far as I know (that'd be a change to windows, not an update to python).
(Sep-28-2016, 02:58 PM)metulburr Wrote: [ -> ]
(Sep-28-2016, 02:55 PM)sparkz_alot Wrote: [ -> ]An example is, starting with v3.3, windows now recognizes the shebang line, so now
Python Code: (Select All)
#! /usr/bin/env python3 

works in both Linux and Windows.

I have never heard of this. How exactly would windows know where to find python as the shebang line path is linux binaries directory?

It's here (about halfway down) https://docs.python.org/3/using/windows.html, I took the liberty of using their sample and ran it using python, python2 and python3 in that order (note: I do not have python v2)

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\>cd python
C:\Python>test.py
hello from Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]
C:\Python>test.py
Requested Python version (2) is not installed
C:\Python>test.py
hello from Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]
C:\Python>
pretty cool :cool:
Thanks for splitting this from the original thread, was wondering if it could be done :)