Ast*_*oAT 5 python-3.x scikit-learn jupyter-notebook
我scikit-learn在 Jupyter 笔记本中导入时遇到问题,我不知道为什么这不起作用。我不记得以前使用其他软件包时遇到过这个问题。我已经看到几个线程与这个特定模块有类似问题的人有类似的问题,但是建议的检查都没有解决我的问题,也没有告诉我可能有什么问题。我试图在下面添加尽可能多的信息,试图找出为什么这拒绝工作。
我使用 anaconda 来管理我的包,scikit-learn安装方式如下:
source activate python3
conda install scikit-learn
Fetching package metadata .............
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /Users/overaa/anaconda/envs/python3:
#
scikit-learn 0.19.1 py36hffbff8c_0
Run Code Online (Sandbox Code Playgroud)
现在,如果我直接从命令行启动 python 会话,import sklearn将按预期工作并且模块已正确导入。在 Jupyter 中做同样的事情会导致
ModuleNotFoundError: No module named 'sklearn'
我已经检查了很多东西。首先,确保我的 jupyter 和 jupyter-notebook 指向正确的环境
which jupyter
/Users/user_name/anaconda/envs/python3/bin/jupyter
which jupyter-notebook
/Users/user_name/anaconda/envs/python3/bin/jupyter-notebook
Run Code Online (Sandbox Code Playgroud)
所以笔记本检查出来了。
然后从命令行 python 会话(在与我启动笔记本的环境相同的环境中):
>>> import sklearn
>>> sklearn.__file__
'/Users/user_name/anaconda/envs/python3/lib/python3.6/site_packages/sklearn/__init__.py'
Run Code Online (Sandbox Code Playgroud)
所以scikit-learn也安装在正确的环境中。
现在比较我正在运行相同的版本;
从命令行:
>>> print(sys.version)
3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
Run Code Online (Sandbox Code Playgroud)
从 Jupyter 笔记本:
print(sys.version)
3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
Run Code Online (Sandbox Code Playgroud)
比较包路径。
从命令行:
import site
print(site.getsitepackages())
['/Users/user_name/anaconda/envs/python3/lib/python3.6/site-packages']
Run Code Online (Sandbox Code Playgroud)
从 Jupyter 笔记本:
import site
print(site.getsitepackages())
['/Users/user_name/anaconda/envs/python3/lib/python3.6/site-packages']
Run Code Online (Sandbox Code Playgroud)
同样对于我的 sys.path
命令行:
>>> print(sys.path)
['', '/Users/user_name/anaconda/envs/python3/lib/python36.zip',
'/Users/user_name/anaconda/envs/python3/lib/python3.6',
'/Users/user_name/anaconda/envs/python3/lib/python3.6/lib-dynload',
'/Users/user_name/anaconda/envs/python3/lib/python3.6/site-packages',
'/Users/user_name/anaconda/envs/python3/lib/python3.6/site_packages/setuptools-27.2.0-py3.6.egg']
Run Code Online (Sandbox Code Playgroud)
朱庇特:
print(sys.path)
['', '/Users/user_name/anaconda/envs/python36/lib/python36.zip',
'/Users/user_name/anaconda/envs/python36/lib/python3.6',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/lib-dynload',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg',
'/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages/IPython/extensions',
'/Users/user_name/.ipython']
Run Code Online (Sandbox Code Playgroud)
所以一切都是一样的,除了setuptools命令行版本和 Jupyter 版本包含不同的目录。我还注意到它scikit-learn似乎安装在子目录中,site_packages而不是site-packages子目录中,这是 Jupyter 似乎正在寻找模块的地方。所以我尝试了两件事。首先,JUPYTER_PATH在运行我的笔记本之前,将路径添加到我的 .bash_profile 中。
export JUPYTER_PATH="/Users/user_name/anaconda/envs/python36/lib/python3.6/site-packages:$JUPYTER_PATH"
Run Code Online (Sandbox Code Playgroud)
sys.path在尝试导入之前,我还尝试将它直接添加到我的 jupyter 笔记本中scikit-learn
sys.path.append("/Users/user_name/anaconda/envs/python3/lib/python3.6/site_packages")
Run Code Online (Sandbox Code Playgroud)
两者都无济于事,sklearn仍然拒绝在我的笔记本中导入。
关于为什么scikit-learn没有导入我的 Jupyter Notebook 的任何想法将不胜感激。还有更多我可以检查的东西吗?我如何让这个工作?
我试过激活/停用我的环境,重新启动内核等,也没有成功。
编辑:我在导入其他包时没有问题,例如numpy或scipy
| 归档时间: |
|
| 查看次数: |
10137 次 |
| 最近记录: |