pythonpytorch

detectron2 installation - No module named 'torch'


I am trying to install detectron2 on ubuntu and face a weird python dependency problem. In short - pytorch is installed (with pip), torchvision is installed (with pip), but when I run

pip install 'git+https://github.com/facebookresearch/detectron2.git'

I get error ModuleNotFoundError: No module named 'torch'

as for dependencies

(detectron2_test) ubuntu@LAPTOP:~$ pip install torchvision
Requirement already satisfied: torchvision in ./detectron2_test/lib/python3.12/site-packages (0.19.1+cu118)
Requirement already satisfied: numpy in ./detectron2_test/lib/python3.12/site-packages (from torchvision) (1.26.3)
Requirement already satisfied: torch==2.4.1 in ./detectron2_test/lib/python3.12/site-packages (from torchvision) (2.4.1+cu118)
(...)

(detectron2_test) ubuntu@LAPTOP:~$ which pip
/home/ubuntu/detectron2_test/bin/pip
(detectron2_test) ubuntu@LAPTOP:~$ which python
/home/ubuntu/detectron2_test/bin/python
(detectron2_test) ubuntu@LAPTOP:~$ which python3
/home/ubuntu/detectron2_test/bin/python3

Any suggestions are appreciated!


Solution

  • This is probably due to the isolation mechanism of the pip building process.

    Basically, the installation requires torch to be installed to work, but recent versions of pip use some isolation that does not allow the build process to access installed packages.

    You can disable that isolation by using this command:

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