3

I'm trying to import torch and I'm getting the next problem:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/torch/__init__.py", line 66, in <module>
    import torch._dl as _dl_flags
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/torch/_dl.so, 2): no suitable image found.  Did find:
    /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/torch/_dl.so: mach-o, but wrong architecture
    /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/torch/_dl.so: mach-o, but wrong architecture

someone knows how can I solve this? Thanks:)

chengal
  • 33
  • 1
  • 6

2 Answers2

3

Try like that:

mkdir test_torch
cd test_torch
python3 -m venv .venv
source .venv/bin/activate
pip install torch torchvision
python3

>>> import torch

Works for me. MacOS 10.13.4, Python 3.6.4

Or like that:

mkdir test_torch
cd test_torch
virtualenv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install torch torchvision
python2

>>> import torch

Works for me. MacOS 10.13.4, Python 2.7.10

If you don't need to use torch then you can install only torchvision

pip install --no-deps torchvision
Greg Eremeev
  • 1,760
  • 5
  • 23
  • 33
  • I'm using python 2.7.. so i have to delete the 3? – chengal May 19 '18 at 13:53
  • @chengal I've added the solution for Python2.x. Try it. – Greg Eremeev May 19 '18 at 14:02
  • @chengal brew install virtualenv – Greg Eremeev May 19 '18 at 14:07
  • `Error: No available formula with the name "virtualenv" ==> Searching for a previously deleted formula (in the last month)... Warning: homebrew/core is shallow clone. To get complete history run: git -C "$(brew --repo homebrew/core)" fetch --unshallow` – chengal May 19 '18 at 14:19
  • `Error: No previously deleted formula found. ==> Searching for similarly named formulae... ==> Searching local taps... These similarly named formulae were found: pyenv-virtualenv ✔ pyenv-virtualenvwrapper ✔ To install one of them, run (for example): brew install pyenv-virtualenv ✔ ==> Searching taps... ==> Searching taps on GitHub... Error: No formulae found in taps.` – chengal May 19 '18 at 14:19
  • @chengal brew install pyenv-virtualenv – Greg Eremeev May 19 '18 at 14:26
  • I did it. Installed also pyenv-virtualenvwrapper. – chengal May 19 '18 at 14:29
  • Also, pip install virtualenv. After that you could try commands which are above, in the answer – Greg Eremeev May 19 '18 at 14:43
  • `Requirement already satisfied: virtualenv in /Users/Gal_Chen/Library/Python/2.7/lib/python/site-packages (16.0.0)`. this was install' and the commands in the answer still doesn't work – chengal May 19 '18 at 14:49
  • Then pip uninstall virtualenv && sudo pip install virtualenv – Greg Eremeev May 19 '18 at 14:51
  • thank you very much. that worked:) but what is that virtual env? I will need to use this every time that i want to use torch? – chengal May 19 '18 at 14:54
  • @chengal if it works then mark the answer as a correct answer. You need to understand what vertualenv is and why developers use it. You need to create virtualenv every time when you wanna create a new project to install all Python packages there. https://docs.python.org/3/tutorial/venv.html – Greg Eremeev May 19 '18 at 14:58
  • ok. thank you:) and if I write a python script how can I use torch? – chengal May 19 '18 at 15:04
  • Why do we need to use a virtual environment? I intend on only using one version of PyTorch. At the very least just get it working first – JobHunter69 Jun 04 '19 at 04:44
  • @Goldname https://realpython.com/python-virtual-environments-a-primer/#why-the-need-for-virtual-environments – Greg Eremeev Jun 04 '19 at 10:20
  • @GregEremeev I know, I read that. I am not looking on how to get it working with best practices. I just want it to work right now, installed on the system level, but it doesn't work – JobHunter69 Jun 06 '19 at 17:11
0

Try

brew install libomp

should solve the problem

Prakhar Agarwal
  • 2,724
  • 28
  • 31