How Should I Set Default Python Version In Windows?

382,444

Solution 1

This is if you have both the versions installed.

Go to This PCRight-clickClick on PropertiesAdvanced System Settings.

You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.

You will see a top half for the user variables and the bottom half for System variables.

Check the System Variables and double-click on the Path (to edit the Path).

Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.

Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.

Solution 2

The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.

Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.

Here's how to check if the launcher is registered correctly from the console:

C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*

Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.

For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.

py.exe can also be run with switches to force a Python version:

py -3 script.py       # select latest Python 3.X version to be used.
py -3.6 script.py     # select version 3.6 specifically.
py -3.9-32 script.py  # select version 3.9 32-bit specifically.
py -0                 # list installed Python versions (latest PyLauncher).

Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.

Solution 3

Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'

py -2

If you need to switch from python 2.x to python 3.x you will have to use '-3' switch

py -3

If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.

Solution 4

If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.

Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)

enter image description here

Solution 5

If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).

As a user

  • Always type py instead of python when running a script from the command line.

  • Setup your "Open with..." explorer default program association with C:\Windows\py.exe

  • Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional):

    ftype Python.File="C:\windows\py.exe" "%L" %*

    ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*

  • Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.7). You can see what version of python is your default by typing py. You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).

  • If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.

  • List the installed versions of python with py -0.

As a script writer

  • Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:

    #!/usr/bin/env python3

  • You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).


See also

Share:
382,444

Related videos on Youtube

Author by

rooney

Updated on February 09, 2022

Comments

  • rooney 8 months

    I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.

    When I run python in cmd, it displays the python version 2.6, which is what I want!
    But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.

    import sys
    print (sys.version)
    

    What's going on here?

  • rooney over 11 years
    I have already set the environment var correctly,for my question,it doesn't work in the bat file
  • rooney over 11 years
    thank you anyway!I find the right way,I modify the value of the registry item in HKEY_LOCAL_CLASS\Applications\Python.exe\shell\open\command,‌​and then it works right for bat file
  • Mark Tolonen
    Mark Tolonen over 9 years
    @TomW, next time I'll use my crystal ball. It is quite possible the installer is different for a version just released. This is a two-year old answer. Anymore, I'd use the py.exe launcher that comes with Python 3.3 to control the version of Python a script uses via a shebang comment.
  • Tom W
    Tom W over 9 years
    In hindsight the tone wasn't really appropriate so I apologise - I'd like to amend my comment, which I don't seem to be able to do - although the point of StackOverflow is to be helpful, so I think it's appropriate to point out that the behaviour doesn't seem to be the same anymore.
  • Mark Tolonen
    Mark Tolonen over 9 years
    Also, I think prior to Python 3.3, there wasn't an option to add Python to the PATH, but there is now. However, when Python registers itself with Windows, previously it would only associate one explicit python.exe with file extensions, so last installed with the registration option wins. Python 3.3 changes that with the Python Launcher and any Python version can be explicitly specified with a comment in the script.
  • JinSnow
    JinSnow over 5 years
    I had a Launcher.zip in the python installed folder (same level as the multiple python version). I just unziped the folder. Run cmd, type "py", and the launcher was recognized and send the proper python version.
  • cat40
    cat40 over 5 years
    This doesn't change the behavior when python is called in the command prompt.
  • sasori
    sasori over 5 years
    this one is very useful, no need to edit registry keys...when running python 2 scripts in command line... one can do py -2 test.py ...then by default python test.py if it's python 3 based code
  • Smart Manoj
    Smart Manoj over 5 years
    this will work when we run python with its name only
  • eliteproxy
    eliteproxy over 3 years
    Make sure to get the Scripts part of the path too: C:\Python27;C:\Python27\Scripts\;
  • Datacrawler
    Datacrawler over 2 years
    Oraios @George C.
  • Ryan Plant
    Ryan Plant over 2 years
    One detail to watch out for here: if you did not tick 'Install for all users' during custom installation (or did not install as an administrator), Python will get installed to %LOCALAPPDATA%\Programs\Python, and get added to the PATH in your user environment variables. If the other Python did get installed as an admin/for all users, it will go in C:\Program Files\, and be added to PATH in the system environment variables. Usually, user environment variables override system ones, but PATH is always (system PATH + user PATH). This is what's just tripped me up, so I thought I'd note it here.
  • Naveen Kumar over 2 years
    PY_PYTHON didn't work, removing python(undesirable version) path from PATH variable worked for me.
  • Mark Tolonen
    Mark Tolonen almost 2 years
    Just don't add any of them to the path and use the Python launcher. Why edit the path every time you switch? You can also create virtual environments with the installed python of choice with "py -x.y -m venv <environment>" where x.y is the Python version (must already be installed).

Related