【问题标题】:Jupyter Notebook ImportError: No module named 'sklearn'Jupyter Notebook ImportError:没有名为“sklearn”的模块
【发布时间】:2017-06-29 21:42:45
【问题描述】:

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

我已经用 pip、apt-get 和 conda 安装了 sklearn。还尝试了 conda upgrade 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 notebook。

有什么想法吗?

【问题讨论】:

  • 你在这个环境中安装了jupyter notebook吗?

标签: scikit-learn jupyter conda


【解决方案1】:

这一般意味着两者不是同一个环境。最好检查一下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

【讨论】:

  • 我尝试按照您的指示进行操作,但即使满足所有条件也无法正常工作。然后我意识到我在我的 conda 安装的 python3 虚拟环境中。所以我退出了那个笔记本。检查了默认的 python 版本(没有 virtualenv),它是 python 2.7 。启动了 REPL,然后启动了 jupyter notebook。 BOOM 能够来自 sklearn。在这里留下评论以防它帮助某人
  • 我认为我有内核问题,但我没有使用 conda,只是普通的旧 pip。解决办法是什么?
【解决方案2】:

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

pip install notebook

【讨论】:

    【解决方案3】:

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

    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 文件夹将包含所有为那个 环境安装的conda 软件包。 在此路径内文件夹 sklearn 我们正在尝试导入

    import sys
    sys.path.append('/home/hafiz031/anaconda3/envs/NLP/lib/python3.8/site-packages')
    

    现在像往常一样导入所需的包:

    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

    【讨论】:

      【解决方案4】:

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

      conda upgrade scikit-learn
      

      【讨论】:

        【解决方案5】:

        你可以在你使用的环境中安装库

        pip install sklearn
        
        conda install sklearn
        

        【讨论】:

          猜你喜欢
          • 2017-08-04
          • 2016-07-10
          • 1970-01-01
          • 2016-07-24
          • 1970-01-01
          • 2020-03-05
          • 2018-10-21
          • 2018-04-13
          • 2019-09-04
          相关资源
          最近更新 更多