Search code examples
pythoninstallationpytorchpip

ModuleNotFoundError: No module named 'torch', but torch is installed


I'm trying to use (and have successfully installed) Layout Parser, which requires detectron2 for certain functionality. While trying to install detectron2, I ran into the following error:

> python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'

[snip]
      ModuleNotFoundError: No module named 'torch'
      [end of output]
[/snip]

Setting the flags as specified in the installation instructions does nothing:

CC=clang CXX=clang++ ARCHFLAGS="-arch x86_64" pip3 install 'git+https://github.com/facebookresearch/[email protected]#egg=detectron2'

[same output]
  • I have torch (2.4.1) and torchvision (0.19.1) installed, and the versions match.
  • I'm on macOS Sequoia 15.1.1
  • I'm using Python 3.10.14 and pip 24.2

Full output of the installation command is at this pastebin.


Solution

  • Modern pip uses build isolation, it uses a transient virtual env to build a wheel. For packages that don't require build dependencies or packages that declare build dependencies in pyproject.toml it's not a problem. But the package detectron2 requires torch and doesn't provide pyproject.toml.

    You can downgrade to older pip version 22 that doesn't build in an isolated environment. Or disable build isolation using --no-build-isolation. The full command should be

    python -m pip install --no-build-isolation 'git+https://github.com/facebookresearch/detectron2.git'