admin管理员组

文章数量:1405165

I created a virtual environment on MacOS and installed pytorch with pip:

python -m env torch-env
source torch-env/bin/activate
pip install torch torchvision torchaudio

Launching python and importing torch fails, however:

Python 3.11.2 (main, Mar  5 2023, 23:08:47) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.2.4 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.

Traceback (most recent call last):  File "<stdin>", line 1, in <module>
  File "/Users/ia/torch-env/lib/python3.11/site-packages/torch/__init__.py", line 1477, in <module>
    from .functional import *  # noqa: F403
  File "/Users/ia/torch-env/lib/python3.11/site-packages/torch/functional.py", line 9, in <module>
    import torch.nn.functional as F
  File "/Users/ia/torch-env/lib/python3.11/site-packages/torch/nn/__init__.py", line 1, in <module>
    from .modules import *  # noqa: F403
  File "/Users/ia/torch-env/lib/python3.11/site-packages/torch/nn/modules/__init__.py", line 35, in <module>
    from .transformer import TransformerEncoder, TransformerDecoder, \
  File "/Users/ia/torch-env/lib/python3.11/site-packages/torch/nn/modules/transformer.py", line 20, in <module>
    device: torch.device = torch.device(torch._C._get_default_device()),  # torch.device('cpu'),
/Users/ia/torch-env/lib/python3.11/site-packages/torch/nn/modules/transformer.py:20: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at /Users/runner/work/pytorch/pytorch/pytorch/torch/csrc/utils/tensor_numpy.cpp:84.)
  device: torch.device = torch.device(torch._C._get_default_device()),  # torch.device('cpu'),

I don't understand why it compiled it with 1.x. Upon checking the pytorch repo, the requirements.txt does not specify a numpy version (neither does setup.py). I can verify that I'm using the right pip and python:

(torch-env) ➜  ~ which pip
/Users/ia/torch-env/bin/pip
(torch-env) ➜  ~ which python
/Users/ia/torch-env/bin/python

I understand I can circumvent this issue by downgrading my numpy but I want to understand what is happening here. Am I supposed to upgrade something that I'm not explicitly upgrading (pip? pybind11?)? Or is this a bug?

Also note that:

(torch-env) ➜  ~ pip list
Package           Version
----------------- --------
filelock          3.18.0
fsspec            2025.3.0
Jinja2            3.1.6
MarkupSafe        3.0.2
mpmath            1.3.0
networkx          3.4.2
numpy             2.2.4
pillow            11.1.0
pip               22.3.1
setuptools        65.5.0
sympy             1.13.3
torch             2.2.2
torchaudio        2.2.2
torchvision       0.17.2
typing_extensions 4.12.2

本文标签: pythonWhy does torch import complain about numpy versionStack Overflow