已解决:如何在jupyter笔记本中导入scikit-learn? [英] SOLVED: How do I import scikit-learn in a jupyter notebook?
问题描述
我刚刚创建了一个使用scikit-learn的新的conda环境.
I just created a new conda environment for using scikit-learn.
为了避免任何依赖关系问题,我使用了 conda install< package>
来安装 scikit-learn
, jupyter
, pandas
等,并且在安装它们时没有任何问题.
To avoid any dependency issues, I used conda install <package>
to install scikit-learn
, jupyter
, pandas
, etc. and have no issues while installing them.
加载环境后,我检查了 sklearn
是否正常工作:
After loading the environment, I checked if sklearn
was working:
$python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>
在这一步,我相信 sklearn
可以使用了.
At this step, I believe sklearn
is ready for use.
不幸的是,我收到一个 ModuleNotFoundError
试图将其导入jupyter笔记本中.我正在同一环境中运行它.
Unfortunately, I am getting a ModuleNotFoundError
trying to import it in a jupyter notebook. I am running it from the same environment.
import sklearn
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn
ModuleNotFoundError: No module named 'sklearn'
我可以在同一笔记本中导入 numpy
和 pandas
,而不会出现任何错误.
I could import numpy
and pandas
in the same notebook without any errors.
在环境中使用软件包对我来说是新手,我正在从经验中学习.请帮助我了解问题及其解决方法.非常感谢.
Using packages in environments is new to me and I am learning from experience. Please help me understand the problem and how to troubleshoot it. Thanks a lot.
推荐答案
Best practice: Install everything via conda or pip3, as mentioned in this answer.
如果这不起作用,请在jupyter笔记本中检查系统路径:
If that didn't work, check the system paths in jupyter notebook:
import sys
sys.path
和系统可执行文件:
sys.executable
这些必须与您当前加载的环境中的python相对应.
These must correspond to the python in your current loaded environment.
对我来说,问题出在jupyter Notebook的内核上.请参阅路径中 kernel.json
文件中的内核规范.您可以从 jupyter kernelspec列表
中找到此文件的目录.我在我的环境中手动将python路径更改为python(这是一个坏主意,但确实可行).
For me, the issue was with the jupyter Notebook's kernel. See the kernel specifications in kernel.json
file in the path. You can find the directory for this file from jupyter kernelspec list
. I manually changed the python path to the python in my environment (this is a bad idea, but it worked).
这篇关于已解决:如何在jupyter笔记本中导入scikit-learn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!