PyTorch installation issues on MacOS through Anaconda

Viewed 746

I am trying to install PyTorch on my Macbook Pro. I had no issues installing NumPy or Matplotlib using the following commands:

conda install numpy
conda install matplotlib

When I then import those into Python console, they work correctly. However, when I try to import PyTorch I get the following error:

(myenv) $ % python
Python 3.9.1 (default, Dec 11 2020, 06:28:49) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jeasl/opt/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/__init__.py", line 189, in <module>
    _load_global_deps()
  File "/Users/jeasl/opt/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/__init__.py", line 142, in _load_global_deps
    ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
  File "/Users/jeasl/opt/anaconda3/envs/myenv/lib/python3.9/ctypes/__init__.py", line 382, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/Users/jeasl/opt/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/lib/libtorch_global_deps.dylib, 10): Library not loaded: @rpath/libomp.dylib
  Referenced from: /Users/jeasl/opt/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/lib/libtorch_global_deps.dylib
  Reason: image not found

I have absolutely no idea what is causing this, even after looking through several forums for answers. When I got to try and reinstall PyTorch, I get this:

(myenv) $ % conda install pytorch
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

So it seems like it is all downloaded correctly - I just can't import it when in the Python console.

Any idea how to get this working correctly?

1 Answers

OP indicates use of Python 3.9 from Anaconda, but the PyTorch installer tool explicitly notes that one must use Python from the Conda Forge channel:

enter image description here

I have no issue with the following environment YAML:

File: pytorch.yaml

channels:
  - pytorch
  - conda-forge
  - defaults
dependencies:
  - python=3.9
  - pytorch
  - torchvision
  - torchaudio
  - numpy
  - matplotlib

created with

conda env create -f pytorch.yaml -n foo
Related