2

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?

Ishan Dutta
  • 897
  • 4
  • 16
  • 36
  • 1
    Can you run your script from the command line (or Powershell) with your virtualenv activated? If so, then VS Code isn't configured to use the virtualenv. – MattDMo Apr 05 '21 at 16:25
  • I am running the script from the VS Code Code Runner Extension, I could not find the `conda` command to run a script. – Ishan Dutta Apr 05 '21 at 16:27
  • 1
    Check that the Code Runner extension is set to use the python executable located in your virtualenv. – MattDMo Apr 05 '21 at 16:28
  • What should be the terminal command to run a file using `conda`? – Ishan Dutta Apr 05 '21 at 16:30
  • 1
    Activate the virtualenv, go to the folder containing your `train.py` file, and type `python train.py`. – MattDMo Apr 05 '21 at 16:33
  • Wow, this worked. I had not activated the environment. Writing `conda activate myenv` solved the issue. Thank you. – Ishan Dutta Apr 05 '21 at 16:35

7 Answers7

15

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>
Gus Fer
  • 151
  • 1
  • 4
2

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.

P_M_
  • 21
  • 2
1

I had the same issue using python 3.11.4, and installing scikit-learn by pip install sklearn. My solution was to use pip install scikit-learn, as shown in the following sequence:

PS C:\Users\Administrador> pip install scikit-learn
Collecting scikit-learn
  Obtaining dependency information for scikit-learn from https://files.pythonhosted.org/packages/77/85/bff3a1e818ec6aa3dd466ff4f4b0a727db9fdb41f2e849747ad902ddbe95/scikit_learn-1.3.0-cp311-cp311-win_amd64.whl.metadata
  Downloading scikit_learn-1.3.0-cp311-cp311-win_amd64.whl.metadata (11 kB)
Requirement already satisfied: numpy>=1.17.3 in c:\users\administrador\appdata\local\programs\python\python311\lib\site-packages (from scikit-learn) (1.25.2)
Collecting scipy>=1.5.0 (from scikit-learn)
  Obtaining dependency information for scipy>=1.5.0 from https://files.pythonhosted.org/packages/04/b8/947f40706ee2e316fd1a191688f690c4c2b351c2d043fe9deb9b7940e36e/scipy-1.11.1-cp311-cp311-win_amd64.whl.metadata
  Downloading scipy-1.11.1-cp311-cp311-win_amd64.whl.metadata (59 kB)
     ---------------------------------------- 59.1/59.1 kB 1.5 MB/s eta 0:00:00
Collecting joblib>=1.1.1 (from scikit-learn)
  Obtaining dependency information for joblib>=1.1.1 from https://files.pythonhosted.org/packages/10/40/d551139c85db202f1f384ba8bcf96aca2f329440a844f924c8a0040b6d02/joblib-1.3.2-py3-none-any.whl.metadata
  Downloading joblib-1.3.2-py3-none-any.whl.metadata (5.4 kB)
Collecting threadpoolctl>=2.0.0 (from scikit-learn)
  Obtaining dependency information for threadpoolctl>=2.0.0 from https://files.pythonhosted.org/packages/81/12/fd4dea011af9d69e1cad05c75f3f7202cdcbeac9b712eea58ca779a72865/threadpoolctl-3.2.0-py3-none-any.whl.metadata
  Downloading threadpoolctl-3.2.0-py3-none-any.whl.metadata (10.0 kB)
Downloading scikit_learn-1.3.0-cp311-cp311-win_amd64.whl (9.2 MB)
   ---------------------------------------- 9.2/9.2 MB 8.4 MB/s eta 0:00:00
Downloading joblib-1.3.2-py3-none-any.whl (302 kB)
   ---------------------------------------- 302.2/302.2 kB 18.2 MB/s eta 0:00:00
Downloading scipy-1.11.1-cp311-cp311-win_amd64.whl (44.0 MB)
   ---------------------------------------- 44.0/44.0 MB 9.5 MB/s eta 0:00:00
Downloading threadpoolctl-3.2.0-py3-none-any.whl (15 kB)
Installing collected packages: threadpoolctl, scipy, joblib, scikit-learn
Successfully installed joblib-1.3.2 scikit-learn-1.3.0 scipy-1.11.1 threadpoolctl-3.2.0

Checking the scikit-learn version in PowerShell:

PS C:\Users\Administrador> python
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>> print(sklearn.__version__)
1.3.0

Checking via cli the scikit-learn version in a VSCode environment:

enter image description here

0

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.

0

I had a similar issue, I tried every possible solution for this, re-installing the package, double checking the python interpreter / kernel, check python and package version, making sure they had similar environments etc. Every time it gave the same error even when the requirement was already satisfied. Eventually this command helped me:

py -m pip install scikit-learn
-2

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

pip install sklearn

-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

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • [answer], [tour], https://stackoverflow.com/editing-help – Yunnosch Feb 20 '22 at 18:52
  • Please don't post code as images. Code should be pasted as text, and formatted using Markdown. (The toolbar in the Stack Overflow editor can help you with this.) Code within images is harder to read, less accessible, can't be copied, and doesn't show up in relevant searches. Please [edit] your post to include the code as text; this will help you get better responses, and help prevent your answer from getting deleted. – Jeremy Caney Feb 21 '22 at 00:49