Jupyter Notebook 导入错误:没有名为“sklearn”的模块

问题描述 投票:0回答:7

我正在尝试在本地计算机上运行。 我收到一个错误 ImportError:仅在 jupyter 笔记本中没有名为“sklearn”的模块 当我在激活和停用 carnd-term1 env 的情况下从命令行使用 python 时,它工作得很好。

我已经用 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 笔记本。

有什么想法吗?

scikit-learn jupyter conda
7个回答
7
投票

这一般说明两者不是同一个环境。最好检查的是

sys.executable
并确保它是您所期望的。如果笔记本没有使用您期望的
sys.executable
,第一步可能是检查您的路径:

which jupyter
which jupyter-notebook

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

conda install notebook

第二个最有可能的是你安装了一个覆盖你的环境的内核规范(例如使用

ipython kernel install --user
)。你可以看到你的内核在哪里:

jupyter kernelspec list

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

conda install ipykernel
ipython kernelspec install --sys-prefix

然后再次检查

jupyter kernelspec list


5
投票

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

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

例如,如果您使用

anaconda
,则
site-packages
文件夹将包含
that
环境的所有 conda 安装的软件包。 在此路径中有我们正在尝试导入的文件夹
sklearn

import sys sys.path.append('/home/hafiz031/anaconda3/envs/NLP/lib/python3.8/site-packages')
现在照常导入所需的包:

import sklearn

参考资料:

  1. scikit-learn 安装的版本和位置

  2. Python:添加到相对于当前运行脚本的 sys.path 的最佳方式


2
投票
如果您使用虚拟环境,那么您需要在您的环境中安装Notebook:

pip install notebook
    

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

conda upgrade scikit-learn
    

0
投票
我几乎尝试了一切,这终于成功了:

将 Jupiter 笔记本内核从

python3.xx.x

(除 
Ipykernel
 之外的任何内容)更改为 
Ipykernel


0
投票
软件包“

sklearn”已被弃用,因此您应该使用“scikit-learn”来代替它。


-1
投票
您可以在您使用的环境上安装库

pip install sklearn conda install sklearn
    
© www.soinside.com 2019 - 2024. All rights reserved.