简体   繁体   中英

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?

according to pypi:

use pip install scikit-learn rather than pip install sklearn

in my experience this works:

C:\Users\gfernandez>pip install sklearn
Collecting sklearn
Using cached sklearn-0.0.post1-py3-none-any.whl
Installing collected packages: sklearn
Successfully installed sklearn-0.0.post1
C:\Users\gfernandez>pip install scikit-learn
Requirement already satisfied: scikit-learn in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (1.1.3)
Requirement already satisfied: scipy>=1.3.2 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (1.9.3)
Requirement already satisfied: numpy>=1.17.3 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (1.23.4)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (3.1.0)
Requirement already satisfied: joblib>=1.0.0 in c:\users\gfernandez\appdata\local\programs\python\python310\lib\site-packages (from scikit-learn) (1.2.0)
C:\Users\gfernandez>

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.

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

pip install sklearn

I had a similar issue and after applying all recommended solutions, the problem persisted until I later discovered my VS-Code Python Interpreter was pointing to a different virtual environment.

I changed it to the working virtual environment and everything worked perfectly well.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM