pythonvisual-studio-codemodulenotfounderror

Why do I get a "ModuleNotFoundError" in VS Code despite the fact that I already installed the module?


I'm trying to debug some python code using VS Code. I'm getting the following error about a module that I am sure is already installed.

Exception has occurred: ModuleNotFoundError
No module named 'SimpleITK'
  File "C:\Users\Mido\Desktop\ProstateX-project\src\01-preprocessing\03_resample_nifti.py", line 8, in <module>
    import SimpleITK as sitk

I installed the module using

sudo pip install SimpleITK

I know that it was installed because I was getting a similar error when I ran the code through the command line, and it was fixed by doing the above. I don't understand why VS code does not recognize that it's already installed.


Solution

  • sudo pip install is most likely installing globally into a Python interpreter that is different than the one that you have selected in VS Code. Please select the Python interpreter you want to use and then install explicitly using that interpreter (if you're not using a virtual environment then use something like /path/to/python -m pip install SimpleITK, although I strongly recommend using a virtual environment and to not install packages globally).