Skip to main content

Fixed: UserWarning: Failed to initialize NumPy: No module named 'numpy'

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A brand-new PyTorch project printed UserWarning: Failed to initialize NumPy: No module named 'numpy' as soon as it imported torch, and turning a tensor into a NumPy array then failed with RuntimeError: Numpy is not available. PyTorch doesn't install NumPy for you. Adding NumPy to the project fixed both.

What I Was Doing​

I made a small project with uv on Python 3.15.2 and added PyTorch 2.15.0, and nothing else:

Terminal
uv init --python 3.15 tensor-demo
cd tensor-demo
uv add torch

The script makes a tensor of three ones, prints it, then prints it as a NumPy array:

to_numpy.py
import torch

numbers = torch.ones(3)
print(numbers)
print(numbers.numpy())
Terminal
uv run python to_numpy.py

What I Expected​

The tensor, then the same three ones as a NumPy array:

Expected output
tensor([1., 1., 1.])
[1. 1. 1.]

What I Got​

A warning first, then the tensor, then an error:

Output
/Users/you/source/tensor-demo/.venv/lib/python3.15/site-packages/torch/_subclasses/functional_tensor.py:377: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at /Users/runner/work/pytorch/pytorch/torch/csrc/utils/tensor_numpy.cpp:82.)
cpu = _conversion_method_template(device=torch.device("cpu"))
tensor([1., 1., 1.])
Traceback (most recent call last):
File "/Users/you/source/tensor-demo/to_numpy.py", line 5, in <module>
print(numbers.numpy())
~~~~~~~~~~~~~^^
RuntimeError: Numpy is not available

The terminal after uv run python to_numpy.py: a UserWarning that NumPy failed to initialize because there's no module named numpy, then tensor([1., 1., 1.]), then a traceback ending in RuntimeError: Numpy is not available.

The warning came from inside PyTorch while it was being imported, before my first line ran. Plain tensor work still went on, so a script that never touches NumPy shows only the warning. numbers.numpy() is where it really broke.

What I Checked​

I asked uv which packages PyTorch itself requires:

Terminal
uv pip show torch
Output
Name: torch
Version: 2.15.0
Location: /Users/you/source/tensor-demo/.venv/lib/python3.15/site-packages
Requires: filelock, fsspec, jinja2, networkx, setuptools, sympy, typing-extensions
Required-by: tensor-demo

The terminal after uv pip show torch: version 2.15.0, requiring filelock, fsspec, jinja2, networkx, setuptools, sympy and typing-extensions, and no numpy.

NumPy isn't on the list, so uv add torch never installed it. The uv add output agreed: it installed 11 packages, and none of them was NumPy.

Why It Happens​

PyTorch can work with NumPy arrays, but it doesn't depend on NumPy, so installing PyTorch doesn't bring it in. When torch is imported, it tries to load NumPy for those conversions and prints the warning if it can't. Anything that needs NumPy, such as tensor.numpy(), then fails with RuntimeError: Numpy is not available.

PyTorch's installation page puts NumPy among the things to have ready: "Please ensure that you have met the prerequisites below (e.g., numpy), depending on your package manager."

You won't see this in projects that already have NumPy through another package, such as Hugging Face Transformers, which depends on it. It shows up in a project that has PyTorch alone.

The warning has been discussed on PyTorch's tracker for years: issue #60252 reports this exact message, and issue #125678 asks for it to go away when NumPy isn't needed. Both are open.

The Fix​

Add NumPy to the project:

Terminal
uv add numpy
Output
Resolved 29 packages in 207ms
Built tensor-demo @ file:///Users/you/source/tensor-demo
Prepared 1 package in 3ms
Uninstalled 1 package in 0.82ms
Installed 2 packages in 25ms
+ numpy==2.5.5
~ tensor-demo==0.1.0 (from file:///Users/you/source/tensor-demo)

The terminal after uv add numpy: uv resolved 29 packages and installed numpy 2.5.5.

uv add also records NumPy in pyproject.toml and uv.lock, so anyone who installs the project gets it too.

After the Fix​

Terminal
uv run python to_numpy.py
Output
tensor([1., 1., 1.])
[1. 1. 1.]

The terminal after the fix: tensor([1., 1., 1.]), then [1. 1. 1.], with no warning and no error.

No warning, and the tensor turned into a NumPy array. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Installing NumPy globally with pip install numpy outside the projectThe project's own environment still doesn't have it, so the warning staysAdd it to the project: uv add numpy
Hiding the warning with Python's warnings filtersWith -W ignore::UserWarning the message went away, but tensor.numpy() still failed with RuntimeError: Numpy is not availableInstall NumPy

Troubleshooting​

SymptomLikely causeFix
UserWarning: Failed to initialize NumPy: No module named 'numpy' on import torchNumPy isn't in the projectuv add numpy
RuntimeError: Numpy is not available from tensor.numpy()The sameuv add numpy

FAQ​

Is the warning harmful if I never use NumPy?​

No. Tensor work runs without it. But many libraries that sit on top of PyTorch convert to NumPy somewhere, so adding it saves surprises later.

Which NumPy version should I install?​

Let uv choose: uv add numpy picked 2.5.5, the newest, which works with PyTorch 2.15.0.

Conclusion​

UserWarning: Failed to initialize NumPy: No module named 'numpy' meant exactly what it said: PyTorch 2.15.0 doesn't install NumPy. uv add numpy put it in the project, and both the warning and RuntimeError: Numpy is not available went away.

Additional Resources​