我正在尝试在本地计算机上运行。 我收到一个错误 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 笔记本。
有什么想法吗?
这一般说明两者不是同一个环境。最好检查的是
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
。
让我们学习解决此类问题的通用方法。解决方案非常简单。基本上分为三个步骤:
查找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
参考资料:
pip install notebook
conda upgrade scikit-learn
将 Jupiter 笔记本内核从
python3.xx.x
(除
Ipykernel
之外的任何内容)更改为
Ipykernel
。
sklearn”已被弃用,因此您应该使用“scikit-learn”来代替它。
pip install sklearn
conda install sklearn