3

When I run my script that uses torch I get the following error:

  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\torchvision\transforms\functional.py", line 168, in to_tensor
img = torch.from_numpy(np.array(pic, mode_to_nptype.get(pic.mode, np.uint8), copy=True))
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Numpy is not available

I just freshly installed numpy and torch today using pip. What could be the issue?

0

1 Answer 1

3

Ok after tracking the error log I found:

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Apparently numpy released a new version (numpy 2.0.0) just two day ago (2024/06/17) and pip by default installs the new version which is not compatible with pytorch yet as I assume.

Downgrading numpy to the last stable version of numpy v1 using the following command fixed the issue:

pip install numpy==1.26.4
Sign up to request clarification or add additional context in comments.

3 Comments

interesting, I looked at the requirements.txt in the PyTorch GitHub repository and it didn't specify any version number for the numpy dependency, probably causing this problem.
It worked for me. Try this py -m pip install numpy Btw, u date PyTorch
Or upgrade pytorch rather than switching to an older numpy

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.