68

I'm trying to find the best way to switch between the two python compilers, 2.7 to 3.3. I ran the python script from the cmd like this:

python ex1.py

Where do I set the "python" environment in the window's environment variable to point to either python 3.3 or 2.7?

I am wondering if there is an easy way to switch between the two versions from the cmd line?

007mrviper
  • 459
  • 4
  • 20
JPC
  • 5,063
  • 20
  • 71
  • 100

8 Answers8

173

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

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 2
    Being compatible with Unix shebang, you can also use `#!/usr/bin/env python2` or `#!/usr/bin/python2`. However, `#!/usr/bin/python` will not make PyLauncher use Python 2. – InQβ Feb 07 '18 at 07:14
84

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 -

  1. Right Click on My Computer and go to Properties.
  2. Go to Advanced System Settings.
  3. Click on Environment Variables and edit PATH and add the path to your Python 3 installation directory.

For example,

enter image description here

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
3

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.

Philip L
  • 339
  • 1
  • 3
  • 14
2

They are 3 ways you can achieve this using the py command (py-launcher) in python 3, virtual environment or configuring your default python system path. For illustration purpose, you may see tutorial https://www.youtube.com/watch?v=ynDlb0n27cw&t=38s

Timothy Mugayi
  • 1,449
  • 17
  • 11
1

simply add both to the env variables and then you have to move the version you want to the top

MOHAMMED NABAWY
  • 221
  • 3
  • 7
0

There is an easier way than all of the above; You can use the PY_PYTHON environment variable. From inside the cmd.exe shell;

For the latest version of Python 2

set PY_PYTHON=2

For the latest version of Python 3

set PY_PYTHON=3

If you want it to be permanent, set it in the control panel. Or use setx instead of set in the cmd.exe shell.

ehambright
  • 1,416
  • 19
  • 27
  • 2
    this doesn't work for me... both pythons are in path and this would be elegant / the desired solution. What do I do to make this work exactly? – user2305193 Jul 19 '20 at 10:49
  • @user2305193 This variable is used by PyLauncher. See my answer. – Mark Tolonen Sep 30 '20 at 18:08
  • and that's called with `py`? – user2305193 Sep 30 '20 at 18:10
  • @user2305193 Its part of pylauncher which is installed with Python 3. If you installed 3.3 or later as a system wide installation it will be on your path. If you didn't you'll need to add it to your path. – ehambright Sep 30 '20 at 20:03
  • @ehambright sorry, please allow the silly question, but how is pylauncher called, like, what do I type into console after setting the local variable (`PY_PYTHON`)? – user2305193 Oct 01 '20 at 08:14
  • 1
    @user2305193 - you type "py" into the console.The name of the launcher is py.exe. If you installed it for all users it will be in %SYSTEMROOT%. If you didn't, I think it will be wherever you installed python 3 to, although im not sure 100% – ehambright Oct 02 '20 at 21:16
0

Are you using Python version 3+?

  1. Go to your project path
  2. Run py -[version_number_here] and hit Enter -> This will open the Python's Terminal (sort of)

Happy Coding

Kenn
  • 587
  • 1
  • 5
  • 7
-5

You can try to rename the python executable in the python3 folder to python3, that is if it was named python formally... it worked for me