Import scikit-learn not working in Jupyter

I’m using macOS Tahoe 26.2
from the terminal I typed in

pip install scikit-learn

My terminal shows the Name, Version, and Location of the installation, but when I went to Jupyter notebook and clicked File | New

I typed in the code below and hit Run, but nothing happened. What did I do wrong?

import sys

print(sys.executable)

You likely installed to a different environment than the one that your Jupyter if you are having import trouble with code that should use scikit-learn. (Sometimes the easiest way around this at this time is to run %pip install scikit-learn first as a new cell in the top of the notebook where you want to use it & then restart the kernel and try the import. Keep in mind that magic symbol in front of pip is important.That is if you don’t mind yet another package installation kicking around your system. You could always uninstall the package in your terminal before trying that, if you care about only installing it in one place.)
However, what you shared is only two lines and don’t reference scikit-learn?

And something should happen if your import isn’t working. You should get a module not found error. That would be something happening. And in that case you should share that specific error traceback with us using fenced code blocks. Nothing at all happening could indicate different issues. It is hard to tell with the disjointed post you have provided thus far.

You should also share the minimal, reproducible code you are trying to run that gives the import error also using fenced in code block. See here or the last three sentences here.

1 Like

Thank you for your advice @fomightez

I didn’t uninstall the package since I don’t know how at this time. But I think now I have scikit learn installed correctly thanks to you based on the test code I got from AI. will see, hopefully, I can get going with my module “Data Analysis and Visualization with Python” which I’m currently learning.

So based on your quoted description where you ran what you did and the screenshot in your last post, I can see the problem now. Your first two cells indicate that anaconda/conda is involved. pip and conda are two very different package managers. Essentially, if you use conda you have to read about how to use it and try to use it exclusively to the extent that is possible. (It isn’t always possible as some packages have no conda recipe.)

Anaconda/conda has very fancy handling of the environments and you should read how to use them and stick to it as that is what you opted into as your primary package manager when you choose to install and use it. You should start with the provided ‘Getting Started’ and follow up on that about installing packages under ‘Package Management’ and please also read about ‘Environments’.

Anaconda/conda makes an isolated environment and so installing with pip in the terminal like you did was not going to work, as you found. However, because you didn’t provide that you were working with Anaconda/conda, you have installed things in a less portable & robust way. Essentially environments should be portable if done right with conda exclusively. You’ve now mixed and matched with the magic variation of the pip install command from in the running notebook. So your import works for now but that won’t be portable. Or even updatable unless you continue to use pip for that particular package. It would have been best to install it from the Anaconda prompt with conda install conda-forge::scikit-learn as the command as the documentation will direct you.

Basically stumbling around like that & mixing-and-matching different package manager is the recipe for making yourself headaches in the long run. Best to pause, read, and learn for going forward.

While you are learning, I highly recommend reading the top answers here as they will help you also manage the environments packages and how they relate to Jupyter and the kernels it uses.

So in the short run things may be working, but you’d do better by eventually making a new environment using Anaconda/conda as you should and installing scikit-learn as you should with the conda install from the conda-forge channel and then connect that to Jupyter. If things work for you for now, eventually could mean much later or when you start on a new computer on a new system. It’ll depend on your needs. Just know though for now there is a proper way and you were not following the expected methods initially. So you may hit hurdles you wouldn’t have if you followed proper approaches. You may not.

1 Like