TensorFlow is one of the most widely used machine learning frameworks, powering countless AI applications across various domains.
When working with TensorFlow, knowing which version you’re using is essential for compatibility with code examples, troubleshooting errors, and utilizing version-specific features.
This guide provides multiple methods to check your TensorFlow version across different installation types and environments. Whether you’re using pip, Anaconda, or building from source, you’ll find the appropriate command to identify your TensorFlow version quickly.
Why Version Checking Matters
Before diving into the methods, it’s worth understanding why checking your TensorFlow version is important:
- Different TensorFlow versions have varying APIs and functionality
- Code samples and tutorials often target specific versions
- Some bugs are version-specific
- TensorFlow 1.x and 2.x have significant differences in syntax and behavior
- Compatibility with other libraries may depend on specific version ranges
Read: How to Install TensorFlow on Ubuntu 22.04
Method 1: Using Python Import Statement
The most direct way to check your TensorFlow version is through Python code.
For TensorFlow 2.x:
import tensorflow as tf
print(tf.__version__)
Alternatively, for TensorFlow 2.x you can use:
import tensorflow as tf
print(tf.version.VERSION)
For TensorFlow 1.x (especially versions below 1.0):
import tensorflow as tf
print(tf.VERSION) # Note: Deprecated in newer versions
This can be executed in a Python interpreter, script, or Jupyter notebook.
Method 2: Using Command Line One-Liners
If you prefer checking directly from your terminal or command prompt:
For Linux/macOS:
python -c ‘import tensorflow as tf; print(tf.__version__)’ # Python 2
python3 -c ‘import tensorflow as tf; print(tf.__version__)’ # Python 3
Read: Mastering Python Virtual Environments: A Comprehensive Guide to venv, pipenv, poetry, and More
For Windows:
python -c “import tensorflow as tf; print(tf.__version__)” # Note the double quotes
Method 3: Using Package Managers
Different package managers provide commands to list installed packages along with their versions.
For pip:
pip show tensorflow
This provides comprehensive information about your TensorFlow installation:
- Name: tensorflow
- Version: 2.3.0
- Summary: TensorFlow is an open source machine learning framework for everyone.
- Home-page: https://www.tensorflow.org/
- Author: Google Inc.
- Author-email: packages@tensorflow.org
- License: Apache 2.0
- Location: /usr/local/lib/python3.6/dist-packages
- Requires: astunparse, wheel, keras-preprocessing, gast, tensorflow-estimator, opt-einsum, tensorboard, protobuf, absl-py, six, wrapt, termcolor, numpy, grpcio, scipy, google-pasta, h5py
- Required-by: fancyimpute
Alternatively, filter for TensorFlow:
pip list | grep tensorflow # Linux/macOS
pip list | findstr tensorflow # Windows
Read: How to install pip on Ubuntu 18.04 or Ubuntu 20.04
For Anaconda:
If you installed TensorFlow through Anaconda:
conda list | grep tensorflow
Read: How to Install Anaconda on Ubuntu 22.04
Method 4: In Jupyter Notebooks
When working in Jupyter notebooks, you have several options:
Option 1: Python Import
import tensorflow as tf
print(tf.__version__)
Option 2: Magic Commands
!pip show tensorflow
Or:
!pip list | grep tensorflow
Environment-Specific Methods
VirtualEnv Installation
After activating your virtual environment:
python -c ‘import tensorflow as tf; print(tf.__version__)’
Or:
pip list | grep tensorflow
Docker Containers
If you’re running TensorFlow in a Docker container, enter the container’s shell and use any of the methods above.
Troubleshooting Version Checking Issues
Module Has No Attribute ‘version’
If you encounter AttributeError: module ‘tensorflow’ has no attribute ‘__version__’, try:
- For older versions (below 0.10): Use tf.VERSION instead
- For TensorFlow 2.x: Use tf.version.VERSION
- Ensure your virtual environment is activated if you’re using one
- Check for potential package conflicts or incomplete installations
Segmentation Fault
If commands like help(tf) cause segmentation faults, you might have compatibility issues between TensorFlow and your hardware/drivers. Try checking the version with simpler commands like tf.__version__.
Handling Multiple TensorFlow Installations
In environments with both CPU and GPU versions installed, or multiple Python versions, be specific about which installation you want to check:
pip show tensorflow # CPU version
pip show tensorflow-gpu # GPU version
pip3 show tensorflow # Python 3 version
Advanced Version Information
For more comprehensive details beyond just the version number:
import tensorflow as tf
help(tf) # Shows extensive information about the TensorFlow module
Or to check specific components:
# Check TensorFlow and Keras versions together
import tensorflow as tf
import keras
print(f”TensorFlow version: {tf.__version__}”)
print(f”Keras version: {keras.__version__}”)
Understanding TensorFlow Version Naming
TensorFlow follows semantic versioning (MAJOR.MINOR.PATCH):
- MAJOR: Indicates incompatible API changes (e.g., TF 1.x vs. 2.x)
- MINOR: Adds functionality in a backward-compatible manner
- PATCH: Backward-compatible bug fixes
FAQ
What’s the difference between tf.version and tf.version.VERSION?
Both provide the TensorFlow version number, but tf.version.VERSION was introduced in later TensorFlow versions as the more explicit, recommended approach. tf.__version__ works across most versions for backward compatibility.
How can I check if I have the GPU version installed?
import tensorflow as tf
print(“GPU available:”, tf.test.is_gpu_available()) # TF 1.x
print(“GPU available:”, len(tf.config.list_physical_devices(‘GPU’)) > 0) # TF 2.x
Do I need to update my TensorFlow version?
Consider updating if:
- You’re encountering bugs fixed in newer releases
- You need features available only in newer versions
- Security vulnerabilities have been patched
- Your current version has reached end-of-life
How can I verify my TensorFlow installation is working correctly?
import tensorflow as tf
# Run a simple operation
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
print(tf.add(a, b)) # Should output tf.Tensor([4. 6.], shape=(2,), dtype=float32)
What if I’m using TensorFlow inside a framework like Keras?
If you’re using the TensorFlow implementation of Keras, you can still check both versions:
import tensorflow as tf
from tensorflow import keras
print(f”TensorFlow: {tf.__version__}”)
print(f”Keras: {keras.__version__}”)
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.