Check Python version from inside script? Run Pythons script in v2 compatibility mode?

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Fri Jul 7 17:49:11 EDT 2017


On Friday, July 7, 2017 at 11:58:33 AM UTC-7, eryk sun wrote:
> On Fri, Jul 7, 2017 at 7:53 AM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:
> > On Fri, 7 Jul 2017 04:30 pm, Ben S. wrote:
> >
> >> Is there a way to execute a python script with v3 python engine in v2
> >> compatibility mode? I am thinking about a command parameter like (python.exe
> >> is v3.*):
> >>
> >>   python.exe -execute_as_v2 myscript.py
> >
> > No. Python 3 is always Python 3, and Python 2 is always Python 2. But what you
> > can do is install both, and then call
> >
> > python2.exe myscript.py
> >
> > python3.exe anotherscript.py
> 
> Windows Python installs two loaders for each version of Python:
> python.exe and pythonw.exe. No links or copies are created for
> pythonX[w].exe, pythonX.Y[w].exe, or pythonX.Y-32[w].exe. Instead,
> there are separate py.exe and pyw.exe launchers that use the registry
> to find and execute a loader for a given version, e.g.
> 
>     py -2 myscript.py
>     py -3.6-32 anotherscript.py
> 
> As of 3.6, the py launcher defaults to the highest version of Python 3
> that's installed. 64-bit Python is preferred on 64-bit Windows. The
> default version can be overridden by setting the PY_PYTHON environment
> variable.
> 
> That said, you don't have to manually run a script as an argument of
> py.exe or python.exe. For a default Python 3 installation, if the
> PATHEXT environment variable contains ".PY", then you can run
> "script.py" as
> 
>     script arg1 ... argN
> 
> in CMD or PowerShell. If a script has a Unix shebang, the launcher
> will read it to run the required version of Python, if that version is
> installed.

Is there any particular reason the Windows python does it that way?  Certainly it wouldn't be too difficult to include a "python2.exe" and "python3.exe", even as symbolic links.



More information about the Python-list mailing list