简体   繁体   中英

ModuleNotFoundError: 'sklearn' in Jupyter notebook

Using Conda (4.8) on pyhthon 3.7, on Win10. I have scikit learn installed using conda conda install scikit-learn . Tried a few things: also installed it in the env conda install -n my_env scikit-learn . Also tried installing conda install -c anaconda ipython - nothing has worked.

I can list it:

scikit-learn              0.22             py37h6288b17_0  

But in juypter notebook get error from sklearn.datasets import fetch_lfw_pairs (tried couple other commands too) ModuleNotFoundError: No module named 'sklearn'

But If I use Anaconda UI Navigator to launch notebook everything works fine

Update
I tried this command line option did not work for me, despite plenty effort and help & support from the community (as below). Meanwhile the Jupyter notebook can also be launched from the Anaconda UI itself. That always was working for me - no configuration or setup needed (none) . I have not found any limitations etc with this yet (and you do get the exact same notebook). For advanced/unique use cases where you may need to fine tune your configuration cmd line could be helpful, I am not there. Happy Coding

Likely, you are loading the wrong kernel when you start your notebook. Here is a barebones way to set up the environment:

conda create -n testenv python=3.7 -y

conda activate testenv

conda install scikit-learn
conda install ipython
conda install notebook

python -m ipykernel install --user --name testenv

When you click on new in the browser you will have an additional option next to python3, namely the kernel you just registered. I just tested this with anaconda 4.7 and I could import sklearn.

edit:

The code in the answer creates a new python environment. Then, it installs ipython and jupyter notebook in that environment and makes sure that this environment can be used with jupyter notebook (ie registering the ipykernel).

Now of course besides scikit learn, no other libraries have been installed within that specific environment.

So, if you want to use more libraries, you have to go to the command line, activate the environment, and install the libraries you want to use:

conda activate testenv
conda install scipy numpy matplotlib

To then run jupyter notebook from the environment, after you have installed all the things you want (and after having closed the command prompt or having deactivated the environment), you can do

conda activate testenv
jupyter notebook

in the command prompt.

Jupyterlab would usually use the environment inside which you launch it. For example:

  1. If you activate my_env first and then do jupyter lab from terminal, it should detect the environment.
  2. Incase it fails, go to Kernel -> Change Kernel and select the kernel you want to use.

Note: While creating a new kernel, I always use the display-name parameter which helps. You can do something like:

python -m ipykernel install --user --name my_env --display-name "Python (my_env)"

Hope this helps.

To fix this problem, you need to manually install this package in Anaconda .

How do I install? Open your Anaconda Prompt and run the below command:

conda install -c conda-forge scikit-learn

Then restart Jupyter Notebook and import this package.

I think the problem is that the environment is not activated. Try conda activate my_env first, and then type jupyter notebook .

The first thing you can do is:

import sys
print(sys.path)

Check if /path/to/anaconda/envs/my_env/lib/python3.7/site-packages exists in the path.

I find it useful to print the current sys.path so that I know where it is looking at.

conda info --envs

when testEnv exists:

conda activate testEnv

conda list scikit-learn

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