How do you check the installed version of TensorFlow in Python? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you check the installed version of TensorFlow in Python?

message-square
0

Inspect tf.__version__ (import tensorflow as tf; print(tf.__version__)) inside Python, or run importlib.metadata.version("tensorflow") (or python -m pip show tensorflow in your terminal) to check the installed package version instantaneously without loading TensorFlow’s C++ runtime or initializing GPU drivers.

Basic Example

import importlib.metadata

# 1. Recommended (instantaneous, zero GPU/CUDA startup overhead):
print(importlib.metadata.version("tensorflow"))
# Example output: 2.18.0

# 2. Standard runtime check (when TensorFlow is already imported in your script):
import tensorflow as tf

print(tf.__version__)        # Example output: 2.18.0
print(tf.sysconfig.get_build_info().get("cuda_version"))  # Optional: check linked CUDA version
# 3. Quick terminal one-liner (verifies the active virtual environment):
python -c "import tensorflow as tf; print(tf.__version__)"

Key Details & Edge Cases

  • Why importlib.metadata.version("tensorflow") is faster than import tensorflow as tf:
    • Importing tensorflow initializes the C++ TensorFlow runtime, scans CUDA/cuDNN shared libraries, and allocates GPU driver state—which can take several seconds and emit noisy oneDNN or CUDA log warnings. importlib.metadata.version("tensorflow") reads the .dist-info metadata file on disk in microseconds without importing the module.
  • Alternative TensorFlow distribution names (tensorflow-cpu, tensorflow-macos, tf-nightly):
    • If TensorFlow was installed under an explicit variant package name (such as tensorflow-cpu on Linux servers, tensorflow-macos on older Apple Silicon setups, or tf-nightly), importlib.metadata.version("tensorflow") raises PackageNotFoundError even though import tensorflow as tf; tf.__version__ works seamlessly. Check fallback package names or inspect tf.__version__ directly when the distribution name may vary.
  • Comparing TensorFlow versions programmatically (packaging.version.Version):
    • Never compare version strings lexicographically ("2.18.0" > "2.9.0" evaluates to False because '1' < '9' in ASCII!). Parse versions with packaging.version.Version:
      from packaging.version import Version
      import tensorflow as tf
      
      if Version(tf.__version__) >= Version("2.16.0"):
          print("Keras 3 is default in this TensorFlow version")
      
  • Checking the bundled Keras version separately (tf.keras.__version__ vs. keras.__version__):
    • Starting in TensorFlow 2.16+, tf.keras defaults to Keras 3, whose version number (3.x.y) differs from tf.__version__ (2.x.y). Inspect import keras; print(keras.__version__) if you are debugging Keras API compatibility.
  • Verifying python vs. pip environment alignment:
    • Always invoke python -m pip show tensorflow rather than bare pip show tensorflow so you verify the exact Python interpreter your script runs under rather than a global system pip.

Comparison of Methods for Checking the Installed TensorFlow Version

Method Loads TensorFlow C++ / GPU Runtime? Works with tensorflow-cpu / tf-nightly? Returns Best Used When
import tensorflow as tf; tf.__version__ (Recommended in ML scripts) Yes (imports full runtime) Yes (all variants expose tf.__version__) str (e.g., "2.18.0") Scripts/notebooks that already import tensorflow and need runtime version branching or GPU/CUDA diagnostics.
importlib.metadata.version("tensorflow") (Recommended for fast checks) No (reads .dist-info only) Only if exact dist name is passed str (e.g., "2.18.0") CLI tools, health checks, or environment validators needing instant startup with zero CUDA initialization.
python -m pip show tensorflow No (CLI command) Only if exact dist name is passed Terminal table (Name, Version, Location, Requires) Inspecting version, install path (site-packages), and dependencies from the command line.
tf.version.GIT_VERSION / tf.version.COMPILER_VERSION Yes Yes str Debugging custom source builds, nightly commits, or compiler ABI issues.