本文介绍了在virtualenv中运行Jupyter Notebook:已安装的sklearn模块不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了一个virtualenv machinelearn ,并在该环境中安装了一些python模块(pandas,scipy和sklearn).

I have installed a created a virtualenv machinelearn and installed a few python modules (pandas, scipy and sklearn) in that environment.

当我运行jupyter笔记本时,我可以在笔记本中导入熊猫和scipy-但是,当我尝试导入sklearn时,出现以下错误消息:

When I run jupyter notebook, I can import pandas and scipy in my notebooks - however, when I try to import sklearn, I get the following error message:

import sklearn

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-8fd979e02004> in <module>()
----> 1 import sklearn

ImportError: No module named 'sklearn'

我能够在命令行中导入所有模块-所以我知道它们已经成功安装:

I am able to import all modules, at the command line - so I know they have been successfully installed:

(machinelearn) me@yourbox:~/path/to/machinelearn$ python -c "import pandas, scipy, sklearn"
(machinelearn) me@yourbox:~/path/to/machinelearn$ 

如何在运行virtualenv的jupyter笔记本中导入sklearn?

How can I import sklearn in my jupyter notebook running in a virtualenv?

推荐答案

您可能尚未在virtualenv中安装jupyter/IPython.请尝试以下操作:

You probably have not installed jupyter / IPython in your virtualenv. Try the following:

python -c "import IPython"

,并检查在$PATH中找到的jupyter命令是否是venv的bin文件夹中的命令:

and check that the jupyter command found in your $PATH is the one from the bin folder of your venv:

which jupyter

对于Powershell控制台中的Windows用户,您可以使用以下命令检查$env:Path中的jupyter命令是否是venv的Scripts文件夹中的命令:

For windows users in a powershell console, you can use the following to check that the jupyter command in your $env:Path is the one from the Scripts folder of you venv:

get-command jupyter

编辑:如果这是问题所在,只需在您的虚拟设备中运行python -m pip install jupyter.

Edit: if this is the problem, just run python -m pip install jupyter in your venv.

编辑2 :实际上,您可能还需要:

Edit 2: actually you might also need:

python -m ipykernel install --user --name=my-virtualenv-name

,然后在jupyter用户界面中切换名为"my-virtualenv-name"的内核.

and then switch the kernel named "my-virtualenv-name" in the jupyter user interface.

这篇关于在virtualenv中运行Jupyter Notebook:已安装的sklearn模块不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 05:24