Jupyter Notebook ImportError: No module named 'sklearn'

时间:2017-02-11 16:08:42

标签: scikit-learn jupyter conda

I am trying to run on my local machine. I get an error ImportError: No module named 'sklearn' only in jupyter notebook It works fine when I use python from the command line both with the carnd-term1 env activated and deactivated.

I have installed sklearn with pip, apt-get and conda. Also tried conda upgrade scikit-learn. Both with the env active and deactivated.


(carnd-term1) matt@Malta:~/sdc$ conda upgrade scikit-learn
Fetching package metadata .........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /home/matt/anaconda3/envs/carnd-term1:
#
scikit-learn 0.18.1 np112py35_1

(carnd-term1) matt@Malta:~/sdc$ python3
Python 3.5.2 | packaged by conda-forge | (default, Jan 19 2017, 15:28:33) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>

   ...: (carnd-term1) matt@Malta:~/sdc$ ipython
   ...: Python 3.5.2 | packaged by conda-forge | (default, Jan 19 2017, 15:28:33) 
   ...: Type "copyright", "credits" or "license" for more information.
   ...: 
   ...: IPython 5.1.0 -- An enhanced Interactive Python.
   ...: ?         -> Introduction and overview of IPython's features.
   ...: %quickref -> Quick reference.
   ...: help      -> Python's own help system.
   ...: object?   -> Details about 'object', use 'object??' for extra details.
   ...: 
   ...: In [1]: import sklearn
   ...: 
   ...: In [2]: from sklearn.model_selection import train_test_split
   ...: 
   ...: In [3]: (carnd-term1) matt@Malta:~/sdc$ ipython
   ...:    ...: Python 3.5.2 | packaged by conda-forge | (default, Jan 19 2017, 15:28:33) 
   ...:    ...: Type "copyright", "credits" or "license" for more information.
   ...:    ...: 
   ...:    ...: IPython 5.1.0 -- An enhanced Interactive Python.
   ...:    ...: ?         -> Introduction and overview of IPython's features.
   ...:    ...: %quickref -> Quick reference.
   ...:    ...: help      -> Python's own help system.
   ...:    ...: object?   -> Details about 'object', use 'object??' for extra details.
   ...:    ...: 
   ...:    ...: In [1]: import sklearn
   ...:    ...: 
   ...:    ...: In [2]: from sklearn.model_selection import train_test_split
   ...:    ...: 
   ...:    ...: In [3]:

Doesn't work from jupyter notebook.

Any ideas?

5 个答案:

答案 0 :(得分:3)

这通常意味着两者的环境不一样。最好检查的是sys.executable,并确保它符合您的期望。如果笔记本没有使用您期望的sys.executable,那么第一步可能是检查您的路径:

which jupyter
which jupyter-notebook

最可能的问题是笔记本电脑堆栈不在您的conda环境中,您可以通过以下方式解决:

conda install notebook

第二个最可能的是你已经安装了一个覆盖你的环境的kernelspec(例如ipython kernel install --user)。您可以看到内核的位置:

jupyter kernelspec list

要确保在同一个环境中安装了IPython内核,您可以执行以下操作:

conda install ipykernel
ipython kernelspec install --sys-prefix

并在之后再次检查jupyter kernelspec list

答案 1 :(得分:2)

让我们学习解决此类问题的一般方法。解决方案非常简单。基本上分为三个步骤:

  1. 查找 pip 包的安装位置。
  2. 将该目录添加到路径中。
  3. 最后,导入包。

查找pip包的安装位置:

!pip show PACKAGE_NAME

如果您在 ! 中执行该命令,请不要忘记该命令前的 jupyter-notebook。这将为您提供该包的路径(可能还有其他信息)。获取 Location 内给定的路径。

将该目录添加到路径:在您将该包导入到 jupyter 之前,应执行以下代码。

import sys
sys.path.append('path/to/the/package')

现在导入包:

import PACKAGE_NAME

所以,对于sklearn

获取sklearn目录:

!pip show scikit-learn

添加目录:

import sys
sys.path.append('/path/to/sklearn')

导入:

import sklearn

参考:

  1. which version and where scikit-learn is installed

  2. Python: Best way to add to sys.path relative to the current running script

答案 2 :(得分:0)

更新软件包可能会解决您的问题

conda upgrade scikit-learn

答案 3 :(得分:0)

如果使用虚拟环境,则需要将Notebook安装到您的环境中

pip install notebook

答案 4 :(得分:0)

您可以安装使用环境的库

pip install sklearn

conda install sklearn