ModuleNotFoundError:Jupyter笔记本中的'sklearn' [英] ModuleNotFoundError: 'sklearn' in Jupyter notebook

查看:579
本文介绍了ModuleNotFoundError:Jupyter笔记本中的'sklearn'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Win10的pyhthon 3.7上使用Conda(4.8).我已经使用conda conda install scikit-learn安装了scikit学习.尝试了几件事:还将其安装在env conda install -n my_env scikit-learn中.还尝试安装conda install -c anaconda ipython-没有任何效果.

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.

我可以列出它:

scikit-learn              0.22             py37h6288b17_0  

但是在juypter笔记本中出现错误 from sklearn.datasets import fetch_lfw_pairs(也尝试了其他几个命令) ModuleNotFoundError:没有名为"sklearn"的模块

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

但是如果我使用Anaconda UI Navigator来启动笔记本,一切正常

更新
尽管付出了很多努力和帮助&,但我还是尝试了该命令行选项对我不起作用.来自社区的支持(如下所示).同时,Jupyter笔记本也可以从Anaconda UI本身启动.一直对我有用-无需配置或设置(无).我还没有发现与此相关的任何限制(您确实得到了完全相同的笔记本).对于高级/独特用例,您可能需要微调配置cmd行可能会有所帮助,但我不在那里. 快乐编码

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

在浏览器中单击new时,在python3旁边将有一个附加选项,即您刚注册的内核.我刚刚使用anaconda 4.7进行了测试,可以导入sklearn.

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.

答案中的代码创建了一个新的python环境.然后,它将在该环境中安装ipython和jupyter笔记本,并确保该环境可与jupyter笔记本一起使用(即注册ipykernel).

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).

现在当然除了scikit学习外,在该特定环境中还没有安装其他库.

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

要从环境中运行jupyter笔记本,请在安装所有所需的东西之后(并在关闭命令提示符或禁用环境后),可以这样做

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.

这篇关于ModuleNotFoundError:Jupyter笔记本中的'sklearn'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆