ModuleNotFoundError: 'sklearn' in Jupyter notebook

☆樱花仙子☆ 提交于 2020-03-05 02:16:07

问题


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


回答1:


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 (i.e. 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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/59538207/modulenotfounderror-sklearn-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!