'VS Code: ModuleNotFoundError: No module named 'sklearn'

I am working in VS Code to run a Python script in conda environment named myenv where sklearn is already installed. However when I import it and run the script I get the following error:

Traceback (most recent call last):
  File "d:\ML\Project\src\train.py", line 5, in <module>
    from sklearn.linear_models import LinearRegression
ModuleNotFoundError: No module named 'sklearn'

I have tried all possible solutions suggested in the following but nothing worked for me:

Can someone suggest a different way to fix this?



Solution 1:[1]

Have you tried https://code.visualstudio.com/docs/python/environments

Had the same issue and solved it by setting vscode to use my conda environment.

Solution 2:[2]

I also have the same problem, but when I tried this command the error got fixed:

pip install sklearn

Solution 3:[3]

Click on the terminal in the VS Code and run the following command to create the virtual environment in VS Code.

python -m venv path location of the working file\myvenv

in VS Code it will automatically activate.

I have attached an image

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 P_M_
Solution 2 Prachi Bhatt
Solution 3 Henry Ecker