PyTorch: AttributeError: module torch has no attribute version
This error occurs when Python tries to access torch.version as a string instead of the standard dunder attribute torch.version. Use torch.version for the PyTorch version string and torch.version.cuda for the CUDA version.
Root Cause Analysis
This error occurs when Python code attempts to query the installed PyTorch version string using the incorrect syntax torch.version instead of Python's standard dunder attribute torch.__version__.
Root Cause 1: Confusion Between torch.version and torch.version
In Python standard packaging conventions (PEP 396), package versions are exposed through the __version__ string attribute (e.g. torch.__version__ == '2.2.0+cu121'). In PyTorch, torch.version is a submodule containing metadata properties (such as torch.version.cuda and torch.version.git_version), NOT a string. Attempting to print or slice torch.version as a string or calling torch.version when submodules are unimported raises AttributeError: module 'torch' has no attribute 'version'.
Root Cause 2: Incomplete PyTorch Initialization
If the PyTorch import process fails partway through (due to a missing DLL or corrupted shared library), the torch module namespace is only partially initialized, leaving submodules like version unpopulated.
Root Cause 3: Local File Shadowing (torch.py)
If the project contains a local file named torch.py, import torch imports the local file which lacks the version or __version__ attributes.
Root Cause 4: API Drift from Other Frameworks
Other packages (like TensorFlow tf.version.VERSION or OpenCV cv2.getVersionString()) use different naming conventions, leading developers to guess the wrong attribute name.
Reproduction Code (MCVE)
# Simulating access to incorrect version attribute on torch module
class MockTorchModule:
"""Simulates torch namespace with standard __version__ but no top-level 'version' string."""
__version__ = "2.2.0+cu121"
torch_module = MockTorchModule()
# Accessing torch.version triggers AttributeError
if not hasattr(torch_module, "version"):
raise AttributeError("module 'torch' has no attribute 'version'")
Solution 1: Use torch.__version__ to Access PyTorch Version
Always use torch.__version__ to read the PyTorch version string and parse it with packaging.version for version comparisons.
# Solution 1: Official version inspection pattern
class TorchVersionInspector:
__version__ = "2.3.1+cu121"
class version:
cuda = "12.1"
git_version = "abcdef123456"
# Querying PyTorch and CUDA versions correctly
pytorch_version = TorchVersionInspector.__version__
cuda_version = TorchVersionInspector.version.cuda
print("PyTorch Version:", pytorch_version)
print("Compiled CUDA Version:", cuda_version)
assert "2.3.1" in pytorch_version
assert cuda_version == "12.1"
Solution 2: Safe Semantic Version Comparison with packaging.version
Use packaging.version.parse to compare versions reliably without string slicing.
# Solution 2: Robust version comparison helper
def is_pytorch_version_at_least(current_ver: str, target_ver: str) -> bool:
# Clean version string (remove +cu121 suffix)
clean_ver = current_ver.split("+")[0]
curr_parts = [int(p) for p in clean_ver.split(".")[:2]]
target_parts = [int(p) for p in target_ver.split(".")[:2]]
return curr_parts >= target_parts
assert is_pytorch_version_at_least("2.3.1+cu121", "2.0") is True
assert is_pytorch_version_at_least("1.13.1", "2.0") is False
print("Version comparison logic verified successfully.")
Do not use string comparisons like torch.__version__ >= '2.0.0' directly on strings. String comparison evaluates '1.13.0' > '2.0.0' as False, but '2.10.0' < '2.2.0' as True (alphabetical order). Always parse version components as integers or use the packaging library.
Contrast torch.__version__ with torch.version.cuda: torch.__version__ tells you the PyTorch framework release; torch.version.cuda tells you which CUDA toolkit version PyTorch was compiled against.