jupyter-notebookpytorchvirtualenv

No module named 'torch' in Jupyter Notebook although pytorch is installed


The Jupyter Notebook not able to find the torch module,despite I have it installed.

Jupyter Notebook

In the console it has no issue to import torch: command prompt

How do it fix the issue so that I can import torch in Jupyter Notebook?

Note: the kernel "scan_env"(Jupyter) is the exact virtual environment "scan_venv"(command prompt)


Solution

  • For installing from inside an active Jupyter .ipynb file, see here for more about the magic install commands added a few years ago to insure the installation occurs in the proper environment where the kernel is running that underlies the active .ipynb file.

    From the original post, I know you were using pip for that package, and so the magic install command for running inside the active .ipynb file would be:

    %pip install torch torchvision torchaudio