0
0
PytorchHow-ToBeginner · 3 min read

How to Check PyTorch Version Quickly and Easily

To check your installed PyTorch version, run torch.__version__ in Python after importing torch. This will print the exact version number of PyTorch you have.
📐

Syntax

Use the following syntax to get the PyTorch version:

  • import torch: Loads the PyTorch library.
  • torch.__version__: Returns the version string of the installed PyTorch.
python
import torch
print(torch.__version__)
Output
2.0.1
💻

Example

This example shows how to import PyTorch and print its version. It helps confirm which PyTorch version is installed in your environment.

python
import torch
version = torch.__version__
print(f"Installed PyTorch version: {version}")
Output
Installed PyTorch version: 2.0.1
⚠️

Common Pitfalls

Some common mistakes when checking PyTorch version include:

  • Not importing torch before accessing torch.__version__, which causes a NameError.
  • Confusing PyTorch version with CUDA version; torch.__version__ shows PyTorch version only.
  • Running the command outside a Python environment where PyTorch is installed, leading to ModuleNotFoundError.
python
try:
    print(torch.__version__)
except NameError:
    print("Error: torch is not imported. Please import torch first.")

# Correct way:
import torch
print(torch.__version__)
Output
Error: torch is not imported. Please import torch first. 2.0.1
📊

Quick Reference

Summary tips to check PyTorch version:

  • Always import torch before checking version.
  • Use torch.__version__ to get the version string.
  • Run this inside the Python environment where PyTorch is installed.

Key Takeaways

Import torch before accessing its version with torch.__version__.
torch.__version__ returns the installed PyTorch version as a string.
Run the version check inside the Python environment where PyTorch is installed.
Do not confuse PyTorch version with CUDA or other library versions.
Common errors happen if torch is not imported or PyTorch is not installed.