Installing and using scikit-learn

6,242 views
Skip to first unread message

DeTermined

unread,
May 18, 2021, 4:20:51 PM5/18/21
to spyder
I am trying to use scikit-learn within spyder.  I am using spyder 5.0.3 and anaconda 4.10.1.  I installed scikit-learn on an anaconda prompt with the following command:

conda install -c intel scikit-learn

I got this command from the scikit documentation.  Anaconda shows that version 0.24.1 of scikit-learn is installed.

In spyder the following command:

from sklearn.preprocessing import MinMaxScaler

gives me "No module named 'sklearn'".  What is wrong?

yfprojects

unread,
May 19, 2021, 2:19:41 AM5/19/21
to spyder
Hello,

does the following command work in the anaconda environment you installed `scikit-learn` in?

  • python -c "import sklearn; sklearn.show_versions()"
Kind regards
yfprojects

DeTermined

unread,
May 19, 2021, 12:03:01 PM5/19/21
to spyder
On an anaconda command prompt, I get this:

(base) C:\Users\daton>python -c "import sklearn; sklearn.show_versions()"

System:
    python: 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\daton\anaconda3\python.exe
   machine: Windows-10-10.0.19041-SP0

Python dependencies:
          pip: 21.0.1
   setuptools: 52.0.0.post20210125
      sklearn: 0.24.1
        numpy: 1.20.2
        scipy: 1.6.2
       Cython: 0.29.23
       pandas: 1.2.4
   matplotlib: 3.3.4
       joblib: 1.0.1
threadpoolctl: 2.1.0

Built with OpenMP: True

DeTermined

unread,
May 19, 2021, 1:39:25 PM5/19/21
to spyder
yfprojects,

I think I understand where you are going.  The anaconda prompt has a sys.path that includes anaconda3\\lib\\site-packages, where the scikit-learn package is.  In spyder the sys.path does not include the anaconda\\lib\\site-packages.  In Sypder if I add the missing folders to sys.path, spyder does get further.  With the updated sys.path, the error in sypder is "ImportError: No module name 'sklearn.__check_build._check_build'"

Thanks for the response,
DeTermined

yfprojects

unread,
May 20, 2021, 2:01:56 AM5/20/21
to spyder
Hi DeTermined,

another person posted a similar issue here in the group. I actually don't know what the problem is, but it seems to related to spyder. You could try to type in the import manually into the qtconsole which is the backend of spyders console. You can start it with `jupyter qtconsole` in the anaconda prompt.

It would also be great if you opened a Issue for spyder's repository on github.

Kind regards
yfprojects

DeTermined

unread,
May 20, 2021, 12:31:27 PM5/20/21
to spyder
yfprojects,

I figured out what I did and how to fix it.

First, I installed anaconda.  From the anaconda browser I installed and launched spyder.  When I launched, I got a message that 5.0 was available.  I was excited to get the latest version.  So I went to the main spyder page got the latest and installed it.  I merrily went along with my new spyder 5 for a while, not noticing my mistake, until I tried to import scikit-learn.

Fixing my mess was not too hard.  In Windows I uninstalled spyder 5.  Then I went back to the anaconda prompt and installed it there, and I was back in business.

Thank you again - I would not have figured it out if you had not suggested going to the anaconda environment!

DeTermined

yfprojects

unread,
May 21, 2021, 1:53:17 AM5/21/21
to spyder
DeTermined,

I am glad your problem is solved.

Kind regards
yfprojects

Philip Yip

unread,
May 22, 2021, 11:38:47 AM5/22/21
to spyder
The standalone Spyder 5.0.3 Windows installer includes its own Python environment which is independent of Anaconda... It includes python, numpy, matplotlib, pandas but does not include all the other additional data science libraries such as sklearn. The default Python environment is custom (Python 3.7.9) as shown at the bottom.

Untitled 20.jpg

More details about what is included is given in Help and then Dependencies (note sklearn is not listed):

vlcsnap-2021-05-22-08h54m54s684.png

vlcsnap-2021-05-22-08h54m59s988.png

To use sklearn and other data science libraries with the latest version of Spyder 5 you need to create a conda environment. I recommend cloning the base conda environment. To make the environment appropriate for Spyder 5 you need to remove the old spyder-kernels (for Spyder 4) and install the latest ones from conda forge (for Spyder 5).

Open up the Anaconda Powershell Prompt and type in:
conda create --name spyder5env --clone base 
conda activate spyder5env 
conda remove spyder-kernels 
conda install --channel conda-forge spyder-kernels

Install any other packages (avoid pip where possible and instead preferentially install from conda or conda forge).

Type the following into the address bar in Windows Explorer using the Windows 10 Environmental variable:

%UserProfile%\anaconda3\envs\spyder5env

This should take you to the new env folder where you can copy the address:
vlcsnap-2021-05-22-09h05m55s490.png


Launch Spyder 5 and select Tools and Preferences:
vlcsnap-2021-05-22-08h56m03s302.png

Select Python Interpreter to the left hand side and then select Use the following Python interpreter. Paste the copied path:
vlcsnap-2021-05-22-09h06m42s702.png
Add the \ behind the path name and the python.exe should auto-populate:
vlcsnap-2021-05-22-09h06m50s549.png

Apply the changes and close and relaunch Spyder. Notice that it is now using the custom conda environment called spyder5env and as this was made from a clone of the base Anaconda installation it contains all the data science libraries:
Untitled 21.jpg

Note if you have Spyder4 installed (with Anaconda3) you will need to change the Python interpreter back to the base environment as the newer spyder-kernels are incompatible with the older version of Spyder.

vlcsnap-2021-05-22-11h07m13s148.png

vlcsnap-2021-05-22-11h07m49s971.png
vlcsnap-2021-05-22-11h09m29s663.png
I made a tutorial video doing this:

yfprojects

unread,
May 25, 2021, 9:53:27 AM5/25/21
to spyder
Hi Philip,

The problem of this thread is already solved. It also did not relate to the standalone version of Spyder.

Kind regards
yfprojects
Reply all
Reply to author
Forward
0 new messages