Search code examples
pythonjupyter-notebookpytorchtorchdetectron

Torch is installed but I'm unable to import it in a computer vision python project in Jupyter notebook


I'm working on a computer vision project using detectron2. I'm having trouble installing torch or importing it into my jupyter notebook. I'm working on a mac that is running MACOS Catalina, Python3 version in 3.8.2 and I'm using Anaconda for my Development environment. In the screenshot, it says torch is installed but it throws error while importing it enter image description here


Solution

  • You have installed torch for python at /usr/local/lib/python3.9/site-packages

    but as you said yourself, you are

    using Anaconda for my Development environment

    So if your jupyter notebook is really configured to use your python installation that came alongside anaconda, then you need to do

    conda install pytorch torchvision torchaudio cudatoolkit=10.1 -c pytorch
    

    Note:

    1. Make sure which python interpreter your jupyter is actually using by running

      import sys; print(sys.executable)

    2. Make sure that you install torch for the correct conda environment (above command will also indicate which one that is)

    3. Set the cudatoolkit version according to your needs. There is also a simple interface on the official website to get the correct command