[英]ModuleNotFoundError: 'sklearn' in Jupyter notebook
Using Conda (4.8) on pyhthon 3.7, on Win10.在 Win10 上的 Python 3.7 上使用 Conda (4.8)。 I have scikit learn installed using conda conda install scikit-learn
.我使用 conda conda install scikit-learn
。 Tried a few things: also installed it in the env conda install -n my_env scikit-learn
.尝试了几件事:还将它安装在 env conda install -n my_env scikit-learn
。 Also tried installing conda install -c anaconda ipython
- nothing has worked.还尝试安装conda install -c anaconda ipython
- 没有任何效果。
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'但是在 juypter notebook 中from sklearn.datasets import fetch_lfw_pairs
得到错误(也尝试过其他几个命令) ModuleNotFoundError: No module named 'sklearn'
But If I use Anaconda UI Navigator to launch notebook everything works fine但是如果我使用 Anaconda UI Navigator 来启动 notebook 一切正常
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.同时,也可以从 Anaconda UI 本身启动 Jupyter notebook。 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.对于您可能需要微调配置 cmd 行的高级/独特用例可能会有所帮助,我不在那里。 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.当您在浏览器中单击new
时,您将在 python3 旁边有一个附加选项,即您刚刚注册的内核。 I just tested this with anaconda 4.7 and I could import sklearn.我刚刚用 anaconda 4.7 测试了这个,我可以导入 sklearn。
The code in the answer creates a new python environment.答案中的代码创建了一个新的 python 环境。 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).然后,它会在该环境中安装 ipython 和 jupyter notebook,并确保该环境可以与 jupyter notebook 一起使用(即注册 ipykernel)。
Now of course besides scikit learn, no other libraries have been installed within that specific environment.现在当然除了 scikit learn 之外,在该特定环境中还没有安装其他库。
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然后从环境中运行 jupyter notebook,在你安装了你想要的所有东西之后(并且在关闭命令提示符或停用环境之后),你可以做
conda activate testenv
jupyter notebook
in the command prompt.在命令提示符中。
Jupyterlab would usually use the environment inside which you launch it. Jupyterlab 通常会使用您启动它的环境。 For example:例如:
my_env
first and then do jupyter lab
from terminal, it should detect the environment.如果您先激活my_env
然后从终端执行jupyter lab
,它应该检测环境。Kernel -> Change Kernel
and select the kernel you want to use.如果失败,请转到Kernel -> Change Kernel
并选择要使用的内核。 Note: While creating a new kernel, I always use the display-name
parameter which helps.注意:在创建新内核时,我总是使用display-name
参数来帮助。 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 .要解决此问题,您需要在Anaconda 中手动安装此包。
How do I install?如何安装? Open your Anaconda Prompt and run the below command:打开您的Anaconda Prompt并运行以下命令:
conda install -c conda-forge scikit-learn
Then restart Jupyter Notebook and import this package.然后重启Jupyter Notebook并导入这个包。
I think the problem is that the environment is not activated.我认为问题是环境没有激活。 Try conda activate my_env
first, and then type jupyter notebook
.先尝试conda activate my_env
,然后输入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.检查/path/to/anaconda/envs/my_env/lib/python3.7/site-packages
中是否存在/path/to/anaconda/envs/my_env/lib/python3.7/site-packages
。
I find it useful to print the current sys.path
so that I know where it is looking at.我发现打印当前的sys.path
很有用,这样我就知道它在看哪里。
conda info --envs
conda activate testEnv
conda list scikit-learn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.