首页 文章

Jupyter Notebook ImportError:没有名为'sklearn'的模块

提问于
浏览
3

我想在我的本地机器上运行 . 我得到一个错误ImportError:只有在jupyter笔记本中没有名为'sklearn'的模块当我使用命令行中的python并且carnd-term1 env被激活和停用时它工作正常 .

我用pip,apt-get和conda安装了sklearn . 还试过conda升级scikit-learn . env活动和停用都有 .


(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]:

从jupyter笔记本不起作用 .

有任何想法吗?

1 回答

  • 3

    这通常意味着两者的环境不同 . 最好检查的是 sys.executable 并确保它符合您的期望 . 如果's the notebook that'未使用您期望的 sys.executable ,则第一步可能是检查您的PATH:

    which jupyter
    which jupyter-notebook
    

    最可能的问题是笔记本电脑堆栈不在您的conda env中,您可以使用以下方法解决:

    conda install notebook
    

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

    jupyter kernelspec list
    

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

    conda install ipykernel
    ipython kernelspec install --sys-prefix
    

    然后再检查 jupyter kernelspec list .

相关问题