HOWTO · Python

How to Change Python Version

Learn how to choose an installed Python interpreter for a command, shell, project environment, or script, then verify which version runs and troubleshoot unavailable commands.

On this page

Changing the Python version for one command does not have to change the interpreter used by the rest of your computer. Multiple Python interpreters can coexist. Choose an installed interpreter explicitly for a one-time command, use a shell alias for a shell-specific shortcut, create a project environment with the interpreter you want, or configure a script launcher. Then verify which interpreter actually ran. Avoid replacing an operating system’s Python installation or changing a machine-wide default unless you understand which tools depend on it.

Choose an installed Python interpreter for a command

Use the executable name or full path for the interpreter you intend to run. Depending on the operating system and installation, commands may include python, python3, or a versioned name such as python3.12. These names are not guaranteed to exist or refer to the same installation. On Windows, the Python launcher may also let you request an installed version with py -3.12; availability depends on how Python was installed.

A version manager such as pyenv can install or select interpreters, but it is not a Git feature. After installing the required interpreter through your chosen tool, pyenv local 3.12.2 selects that version for the current directory and its descendants by writing a .python-version file. pyenv global 3.12.2 sets the selection used outside directories with a more specific local selection. These commands select among versions available to pyenv; they do not change operating-system components or guarantee that every application uses the selection. If a requested version is unavailable, install it with the version manager first or choose an interpreter that is already installed.

For a command that needs no persistent selection, invoke the desired executable directly, for example python3.12 your_script.py. This limits the choice to that command and makes the selected interpreter visible in the command itself.

Use a shell alias to change command behavior

An alias can make a short command name resolve to a chosen executable in a particular shell. In a POSIX-style shell such as Bash, alias python=python3.12 affects command lookup in that shell session. To keep the alias in future interactive Bash sessions, put it in the appropriate startup file and open a new shell. It does not rewrite scripts, change other users’ shells, or configure applications that bypass shell lookup. Shell startup files and alias rules differ, so do not assume this syntax works in every shell.

Windows command shells use different mechanisms. In Command Prompt, a doskey macro such as doskey python=py -3.12 $* applies to that Command Prompt session; it is not a system-wide Python setting and does not affect every process. PowerShell has its own alias and function rules, and a PowerShell alias does not automatically pass arguments like an executable wrapper. When a shortcut must work predictably across tools or users, call the intended interpreter explicitly or configure the relevant launcher rather than relying on a temporary shell alias.

Select an interpreter for a project environment

A virtual environment isolates a project’s packages and command lookup. Create it with the intended installed interpreter, for example python3.12 -m venv .venv. The venv module does not install Python: if that executable is missing, choose or install an interpreter first. The environment is based on the interpreter that created it, so changing a global selection later does not convert an existing environment to another version. Create a new environment with the new interpreter when the project needs a different Python version.

Activation is optional but convenient. On POSIX shells, use source .venv/bin/activate; in Windows PowerShell, use .venv\Scripts\Activate.ps1; in Command Prompt, use .venv\Scripts\activate.bat. Activation adjusts the current shell’s command search path. You can avoid activation by invoking the environment’s Python directly: .venv/bin/python on POSIX or .venv\Scripts\python.exe on Windows. Keep the environment directory out of version control and recreate it from the project’s dependency declarations when needed.

Select an interpreter when launching a script

On Unix-like systems, a script can begin with a shebang such as #!/usr/bin/env python3. When the script is launched as an executable, the operating system uses that line to find python3 through the environment’s PATH; the selected version therefore depends on command resolution in the launching environment. A shebang does not install an interpreter, and a version-specific command such as python3.12 works only where that command is installed. Make the file executable if you want to launch it directly. Alternatively, run python3.12 script.py to choose the interpreter explicitly.

Windows does not generally use Unix shebang processing when a script is started by double-clicking or through file association. The Python launcher can use a version selector in a script, such as #! python3.12, when the script is launched through the launcher and that version is installed. Launcher behavior depends on the installation and invocation method. For predictable selection, use py -3.12 script.py or the full path to the intended executable. Do not write a spaced version label as if it were a portable interpreter path.

Verify the selected version and handle unavailable interpreters

Ask the chosen executable for its version with python3.12 --version, python --version, or py -3.12 --version, as appropriate. To see the exact executable and version from inside a Python program, run this self-contained example with the interpreter you want to check. In the supplied runtime, it printed:

import sys

print(sys.executable)
print(sys.version.split()[0])
/usr/local/bin/python3
3.14.7

If the result differs from what you expected, first check which command your shell resolves: command -v python is useful in POSIX shells, while where python works in Command Prompt and Get-Command python in PowerShell. Then check whether a virtual environment is active, whether a local pyenv selection applies in the current directory, and whether your script was launched through a shell, a launcher, or a file association. A “command not found” or “not recognized” message usually means that the requested command is not available through that process’s search path; it does not mean another Python version has been installed automatically. Use an available executable, install the intended interpreter through an appropriate installer or version manager, or correct the relevant environment configuration. Keep system Python settings intact unless you have verified that system tools do not depend on them.