Skip to main content

Switching Python Versions: Comprehensive Guide

ยท 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Managing the active Python version is critical for development, especially when working on projects with different dependencies or legacy requirements. The methods vary significantly depending on whether you are using a dedicated version manager, a package manager, or system-level tools.

Pyenv is the most flexible tool for installing, managing, and switching between multiple Python versions in isolation. It works by manipulating the PATH environment variable to intercept Python commands.

ContextCommandAnnotation
Global Defaultpyenv global 3.11.8Sets the Python version used by default when you are not inside a specific project directory.
Local Projectpyenv local 3.10.12Creates a .python-version file in the current directory, forcing that specific version for the project. Recommended for project work.
Shell Sessionpyenv shell 3.9.18Temporarily overrides the global/local setting for the duration of your current terminal session.
New Versionpyenv install 3.12.1Installs a new, clean Python interpreter into the isolated ~/.pyenv/versions directory.
Verificationpython --version and which pythonUse these to confirm that the python executable points to the pyenv path, not the system path.

2. Using Poetry (For Project-Specific Virtual Environments)โ€‹

Poetry is primarily a dependency manager, but it excels at isolating project environments. It typically uses the Python version specified in the project's pyproject.toml file to create a virtual environment.

ContextCommandAnnotation
Define Versionpython = "^3.11" in pyproject.tomlThis tells Poetry which Python interpreter (from your system or Pyenv) to look for when creating the environment.
Auto-Detect & Createpoetry installPoetry finds an interpreter matching the pyproject.toml constraint and creates a new virtual environment specific to the project.
Switch Interpreterpoetry env use /path/to/pythonExplicitly tells Poetry to switch the project's virtual environment to use a different, specific Python interpreter (e.g., one managed by Pyenv).
Check Active Envpoetry env infoShows the full path and Python version of the active virtual environment for the project.

3. System-Level Changes (Linux/Unix-like Systems)โ€‹

On Unix-like systems (including macOS), you can change the system-wide default Python version, but this is highly discouraged as it can break system tools that rely on a specific Python version (often Python 3.8 or 3.9).

ContextCommandAnnotation
Manual Symlink (Discouraged)ln -sf /usr/bin/python3.11 /usr/bin/pythonCreates a symbolic link, making python point to python3.11. Dangerous on production systems.
Debian/Ubuntusudo update-alternatives --config pythonProvides an interactive way to select the default executable for python (or python3) from installed options.
macOS (via Homebrew)brew install python@3.12 then brew link python@3.12Homebrew installs versions in isolation; the link command creates global symlinks, but it's safer to use Pyenv.

4. Local Changes (Windows)โ€‹

Windows handles Python versions differently, relying on installation order and the execution alias setup.

ContextSolutionAnnotation
Execution AliasCheck the Windows Apps Settings for "Manage app execution aliases."Windows often directs the python command to the Microsoft Store installer. Disable this alias to use your installed versions.
Direct PathUse the full path: C:\Python312\python.exeInstead of relying on the environment PATH, explicitly execute the desired version.
Virtual Environmentspy -3.11 -m venv .venvWindows-specific py.exe launcher allows easy selection of installed versions by running py -<version>.

5. Other Cases (Conda and Docker)โ€‹

Conda (Anaconda/Miniconda)โ€‹

Conda is an environment and package manager popular in data science. It manages its own Python versions completely separate from the system or Pyenv.

  • Create New Environment: conda create -n my_env python=3.10
  • Activate Environment: conda activate my_env
  • Switch Version: Use conda install python=3.12 to change the version within the active environment.

Dockerโ€‹

In a Docker environment, the Python version is determined entirely by the base image specified in your Dockerfile.

  • Switch Version: Change the FROM line in the Dockerfile:

    # Change the tag to switch the version
    FROM python:3.12-slim
  • Rebuild: Run docker build . to create the new image with the desired Python version.

Sourcesโ€‹