PyTorch: RuntimeError: Numpy is not available when interacting with tensors
This error occurs when Python tries to call tensor.numpy() or use NumPy interop functions in PyTorch without the NumPy package installed. Install NumPy via pip install numpy.
Root Cause Analysis
This error occurs when Python code calls .numpy() on a PyTorch tensor (or calls functions that convert tensors to NumPy arrays) in a minimal environment where the numpy package is not installed or failed to import.
Root Cause 1: Minimal Container Without NumPy Dependency
PyTorch can technically operate as a standalone computation engine without NumPy installed. However, calling .numpy(), torch.from_numpy(), or using DataLoader collate functions that invoke NumPy array conversion raises RuntimeError: Numpy is not available! Using numpy with torch requires numpy to be installed.
Root Cause 2: Corrupted or Incompatible NumPy Build
If NumPy was installed with binary incompatibilities (such as installing a CPython 3.11 wheel on Python 3.12 or encountering numpy.core.multiarray failed to import), PyTorch's internal import check fails silently and flags NumPy as unavailable.
Root Cause 3: Using .numpy() in Embedded PyTorch Runtimes
In embedded C++ environments (LibTorch) or stripped micro-services running PyTorch Lite, NumPy is intentionally omitted.
Root Cause 4: Local Module Shadowing (numpy.py)
A local file named numpy.py in the workspace prevents PyTorch from importing the official NumPy library.
Reproduction Code (MCVE)
# Simulating tensor.numpy() call when numpy is unavailable
class MockTorchTensorWithoutNumpy:
def __init__(self, data: list):
self.data = data
def numpy(self):
# PyTorch raises RuntimeError when numpy cannot be imported
raise RuntimeError(
"RuntimeError: Numpy is not available! Using numpy with torch requires numpy to be installed."
)
tensor = MockTorchTensorWithoutNumpy([1, 2, 3])
tensor.numpy()
Solution 1: Install NumPy in the Active Environment
Install standard NumPy into your Python environment using pip install numpy.
import numpy as np
# Solution 1: Verify NumPy installation and tensor interop
# In PyTorch:
# tensor = torch.tensor([10, 20, 30])
# arr = tensor.numpy()
# Standalone simulation demonstrating tensor to numpy array conversion
class SafeTensor:
def __init__(self, values: list):
self.values = values
def to_numpy(self) -> np.ndarray:
return np.array(self.values)
t = SafeTensor([10, 20, 30])
arr = t.to_numpy()
print("Converted array:", arr)
print("Array type:", type(arr).__name__)
assert isinstance(arr, np.ndarray)
Solution 2: Use Native PyTorch Operations or tolist() Without NumPy
If running in a lightweight environment without NumPy, extract values with .tolist() or use pure PyTorch tensor operations.
# Solution 2: Extracting data as native Python primitives without NumPy
raw_values = [42.0, 99.5, -3.14]
# Native python extraction pattern
python_list = list(raw_values)
first_scalar = float(raw_values[0])
print("Native Python list:", python_list)
print("Scalar value:", first_scalar)
assert len(python_list) == 3
assert first_scalar == 42.0
When installing NumPy alongside PyTorch 2.x, be mindful of NumPy 2.0. PyTorch releases prior to 2.3 were compiled against NumPy 1.x and may emit C-API warnings or binary incompatibility errors on NumPy 2.0. Ensure you use numpy<2.0.0 or upgrade to PyTorch 2.4+.
Contrast .numpy() with .tolist(): .numpy() creates a memory-shared NumPy view (zero-copy on CPU); .tolist() creates a brand-new Python list of native Python numbers.