HOWTO · Python

How to Switch Between Python 2 and 3

Choose Python 2 or Python 3 for a command or project on Linux, macOS, and Windows, then verify which interpreter runs your script.

On this page

To run a project with the intended interpreter, invoke its Python 2 or Python 3 command directly, or select it through a project environment. This assumes the required interpreter is installed. Python 2 reached end of life in January 2020, as announced in the Python.org Python 2 sunset notice, so use it only when legacy code requires it; choosing an interpreter does not convert code between versions or restore security support.

Confirm the Python 2 and Python 3 interpreters available

On Linux and macOS, check which commands are installed with python2 --version and python3 --version. A plain python command can point to different versions depending on the operating system and installation, so do not assume it selects the version your project needs. If a command is not found, install that interpreter using a source appropriate for your operating system and project; availability varies by platform.

On Windows, the Python launcher can select an installed version. Run py -2 --version and py -3 --version in Command Prompt or PowerShell. These commands require the corresponding versions to be installed and discoverable by the launcher.

Run Python 2 or Python 3 on Linux and macOS

Start an interactive Python 2 session with python2 or Python 3 with python3. To run a script named app.py, use python2 app.py or python3 app.py, respectively. These explicit commands select the interpreter for that invocation and leave the system-wide default unchanged. If your installation uses different command names, use the executable path for the version you installed.

Run Python 2 or Python 3 on Windows

Use py -2 to start Python 2 or py -3 to start Python 3. Run a script with py -2 app.py or py -3 app.py. If the launcher reports that a requested version is unavailable, check that it is installed and registered with the launcher. A command that works in one shell may also depend on that shell’s PATH configuration.

Keep each project’s interpreter and dependencies isolated

A virtual environment separates a project’s installed packages from other projects. Create it with the interpreter you intend to use, then activate it before installing packages or running the project. For Python 3, the standard library provides venv; for example, python3 -m venv .venv creates an environment when that interpreter includes the module. Python 2 does not provide venv; legacy projects may require a compatible third-party tool such as virtualenv, installed and configured for that Python 2 interpreter. Availability and setup depend on the operating system and installed tooling.

Tools such as pyenv can select Python versions per project on supported systems, while Docker can isolate a project in a container image. Both require additional setup, and a usable Python 2 build or image may not be available for every platform. Isolation helps avoid dependency conflicts, but Python 2 remains unsupported and may be unsafe to expose to untrusted input or networks. Prefer Python 3 for new work.

Check the interpreter used by a script

Use the same command or activated environment that runs the script to check its version: python2 --version, python3 --version, py -2 --version, or py -3 --version. Package installation should use that interpreter too: python3 -m pip install PACKAGE associates pip with Python 3, while py -2 -m pip install PACKAGE targets Python 2 through the Windows launcher when it is available.

You can also put this small check in a script and run it with the command selected above. It prints the major version of the interpreter that is executing it:

from __future__ import print_function

import sys
print("Python", sys.version_info[0])
Python 3

If the reported version differs from the one required by the project, change the command or activate the intended environment, then check again. Interpreter selection chooses what runs the code; it does not make Python 2 syntax or dependencies compatible with Python 3.