Python
version-check
installation
troubleshooting
duplicate

Which version of Python do I have installed?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Checking Python version sounds trivial, but many environment bugs come from using a different interpreter than expected. The command python --version is only part of the answer. A reliable check confirms interpreter version, executable path, and package installer alignment in the same environment.

Step 1: Check Version and Executable Together

Start with both version and resolved binary location.

bash
1python --version
2python3 --version
3which python
4which python3

On Windows PowerShell, use:

powershell
python --version
py --version
Get-Command python

These commands tell you whether shell aliases and launcher behavior match your assumptions.

Step 2: Confirm the Runtime Used by Your Script

Inside Python, print executable and detailed version info.

python
1import platform
2import sys
3
4print("executable:", sys.executable)
5print("version:", sys.version)
6print("version_info:", sys.version_info)
7print("implementation:", platform.python_implementation())
8print("prefix:", sys.prefix)
9print("base_prefix:", sys.base_prefix)

If sys.prefix differs from sys.base_prefix, you are likely inside a virtual environment.

Step 3: Verify Virtual Environment Context

Many issues happen when dependencies are installed globally but code runs in a virtual environment, or vice versa.

bash
1python -m venv .venv
2source .venv/bin/activate
3python --version
4python -c "import sys; print(sys.executable)"

Windows PowerShell activation example:

powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python --version

Always check again after activation because shell state can be stale.

Step 4: Align pip with the Same Interpreter

Never assume pip points to the same interpreter as python. Use module invocation to avoid ambiguity.

bash
python -m pip --version
python -m pip list

This ensures package operations run in the exact interpreter environment that will execute your code.

If you see conflicting paths between pip --version and python -m pip --version, prefer the module form and stop using bare pip in scripts.

Step 5: Handle Multi-Version Tooling

On machines with pyenv, conda, or Windows py launcher, interpreter routing may change by folder or shell.

Pyenv checks:

bash
pyenv versions
pyenv which python

Conda checks:

bash
conda info --envs
python --version

Windows launcher checks:

powershell
py -0p
py -3.11 --version

These commands show which Python installation is selected and where it lives.

CI and Container Checks

In CI and containerized builds, print version diagnostics at startup so failures are traceable.

bash
python --version
python -c "import sys; print(sys.executable)"
python -m pip --version

For Docker images, pin exact base tags and avoid floating tags where possible.

dockerfile
FROM python:3.12.4-slim

Version drift in build agents is one of the most common causes of non-reproducible installs.

Quick Diagnostic Script

You can keep a tiny script in your repo to standardize environment checks.

python
1import os
2import platform
3import sys
4
5print("python:", sys.version.split()[0])
6print("exe:", sys.executable)
7print("impl:", platform.python_implementation())
8print("venv:", sys.prefix != sys.base_prefix)
9print("cwd:", os.getcwd())

Run this in local, CI, and production shells when triaging environment issues.

Shell Alias and PATH Precedence Checks

On some systems, shell aliases can shadow real binaries. If version output looks wrong, inspect alias resolution and path order before reinstalling anything.

bash
type python
echo $PATH

On PowerShell:

powershell
Get-Alias python -ErrorAction SilentlyContinue
$env:Path

This quickly reveals whether launcher rules, aliases, or path precedence are selecting an unexpected interpreter.

Common Pitfalls

  • Trusting python --version without checking executable path.
  • Installing packages with one interpreter and running code with another.
  • Forgetting to activate the intended virtual environment.
  • Assuming python and python3 are equivalent on every system.
  • Ignoring launcher tools such as pyenv, conda, or py that override resolution.

Summary

  • Check version and executable path together, not separately.
  • Use python -m pip to keep installer and runtime aligned.
  • Re-verify environment after virtual environment activation.
  • Include interpreter diagnostics in CI and container startup.
  • Treat Python version checks as part of reproducible build hygiene.

Course illustration
Course illustration

All Rights Reserved.