Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found error in VS code despite the fact that I installed it

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 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

like image 788
An Ignorant Wanderer Avatar asked Jun 19 '19 00:06

An Ignorant Wanderer


People also ask

How do I fix module not found?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

Why does Visual Studio not recognize Python module?

The message simply means that VSCode cannot detect the correct path for a Python module. The cause of "Unresolved Import" could be one of the following reason: VSCode is using the wrong Python path. This is often the case if you're running your code against a virtual environment.

How do I fix the import error in VS Code?

To solve unresolved import error in Python, set your Python path in your workspace settings. If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. Then reload the VSCode, and it will fix that error.


2 Answers

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).

like image 162
Brett Cannon Avatar answered Oct 12 '22 01:10

Brett Cannon


After install new module with pip if vscode not recognize it, reloading vscode may work.

  1. Ensure that the module installed inside virtual environment

Activate virtualenv and use correct way of install module with pip: python3 -m pip install {new_module}

  1. Reload vscode: Ctrl+Shift+P, select Reload window

Now vscode will know new module and autocomplition works.

like image 36
EsmaeelE Avatar answered Oct 12 '22 03:10

EsmaeelE