How to switch between python 2.7 to python 3 from command line?
In case you have both python 2 and 3 in your path, you can move up the Python27 folder in your path, so it search and executes python 2 first.
No need for "tricks". Python 3.3 comes with PyLauncher "py.exe", installs it in the path, and registers it as the ".py" extension handler. With it, a special comment at the top of a script tells the launcher which version of Python to run:
#!python2
print "hello"
Or
#!python3
print("hello")
From the command line:
py -3 hello.py
Or
py -2 hello.py
py hello.py
by itself will choose the latest Python installed, or consult the PY_PYTHON
environment variable, e.g. set PY_PYTHON=3.6
.
See Python Launcher for Windows
For Windows 7, I just rename the python.exe
from the Python 3 folder to python3.exe
and add the path into the environment variables. Using that, I can execute python test_script.py
and the script runs with Python 2.7 and when I do python3 test_script.py
, it runs the script in Python 3.
To add Python 3
to the environment variables, follow these steps -
- Right Click on My Computer and go to
Properties
. - Go to
Advanced System Settings
. - Click on
Environment Variables
and editPATH
and add the path to your Python 3 installation directory.
For example,