Search code examples
pythonupgradepython-venvpython-py

Python update - how to update `py` to point to new version?


I started learning Python on v3.7, and now v3.8 is out.

I've downloaded 3.8.0 and installed it to a new python38 directory alongside my python37 folder. The path was updated and now includes both python v3.7 and v3.8.

How to update py?

If I open up a new Command Window or Git Bash window, python --version is updated, but not py --version, how do I update py to point to the latest version?

Are there any other considerations with respect to virtual environments?

They should remain pointing to the version of python they were built with - is that true of both python and py when a venv is active?


Note: py is a command-line python launcher tool included with Python for Windows as of v3.3


Solution

  • After searching for a while, I stumbled into the answer for the py update part, at least.

    The py command is an executable included with Windows Python distributions starting with Python v3.3. It is called the "Python Launcher for Windows" in the official Python documentation.

    • Note: An official py command is not included with Linux Python distributions, though there is the Py Launcher for Unix available for Linux that can be installed separately and works like the Windows version.

    The documentation on the Python Launcher is pretty complete, but to answer my original question, I had set the environment variable PY_PYTHON to 3.7 to declare the default Python version for py to use.

    I was able to fix the issue by updating this to 3.8 and restarting any terminal and program that uses a terminal to access py/python. There are other Python environment variables so I updated them all:

    UPDATE: I was able to completely remove the PYTHONHOME and PYTHONPATH variables and just use PY_PYTHON to control the default version. You can read more about these variables in the Environment Variables section of the Python Documentation

    • set PY_PYTHON to 3.8
    • set PYTHONHOME to my new Python38 directory
    • for PYTHONPATH added the new python38 and python38/scripts directories.

    Note: After updating environment variables, you will need to restart some programs, and others might require either a logout or full reboot (was true of VS Code on Windows).


    More info on py command and versions

    You can get a list of basic py commands using py --help:

    $ py --help
    Python Launcher for Windows Version 3.8.150.1013
    
    usage:
    C:\WINDOWS\py.exe [launcher-args] [python-args] script [script-args]
    
    Launcher arguments:
    
    -2     : Launch the latest Python 2.x version
    -3     : Launch the latest Python 3.x version
    -X.Y   : Launch the specified Python version
         The above all default to 64 bit if a matching 64 bit python is present.
    -X.Y-32: Launch the specified 32bit Python version
    -X-32  : Launch the latest 32bit Python X version
    -X.Y-64: Launch the specified 64bit Python version
    -X-64  : Launch the latest 64bit Python X version
    -0  --list       : List the available pythons
    -0p --list-paths : List with paths
    

    The version strings above used to launch a particular version of python are what can be put into the PY_PYTHON environment variable. For example, if you just wanted the latest Python3, then use 3, for a particular Python3, use 3.8, or for the 32-bit version, use 3.8-32, etc.