简体   繁体   English

在ipython笔记本上找不到模块

[英]module not found on ipython notebook

I have an ipython notebook which starts with 我有一个以以下内容开头的ipython笔记本

import sklearn

When I run it, I get : 当我运行它时,我得到:

ImportError: No module named sklearn

indeed if I run the following snippet on the notebook, it's not showing scikit_learn: 确实,如果我在笔记本上运行以下代码段,则不会显示scikit_learn:

import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
print installed_packages_list

However when I run the snippet from command line, scikit_learn is in the list. 但是,当我从命令行运行该代码段时,scikit_learn在列表中。

I thought of a version problem (module installed for one version and not on the other one), bur both on notebook and command line, sys.version give 2.7.5 我想到了一个版本问题(模块安装了一个版本,而不是另一个版本),在笔记本和命令行上都可以找到bur,sys.version给出2.7.5。

Any idea about the issue here ? 对这个问题有任何想法吗?

Following cel's comments: 以下cel的评论:

ipython -c "import sys; print(sys.executable)" 

outputs /usr/bin/python (and so does running import sys; print(sys.executable) directly in the notebook) 输出/ usr / bin / python(在笔记本中直接运行import sys; print(sys.executable)也是如此)

Then, /usr/bin/python -m pip install scikit-learn outputs: Requirement already satisfied (use --upgrade to upgrade): scikit-learn in /Users/MyUserName 然后, /usr/bin/python -m pip install scikit-learn输出:已经满足要求(使用--upgrade进行升级):/ Users / MyUserName中的scikit-learn

I was able to fix this error on Ubuntu by adding a cell to the top of the notebook appending the module directory to the path: 通过在笔记本顶部添加单元格并将模块目录附加到路径,我能够在Ubuntu上解决此错误:

import sys
sys.path.append('/usr/lib/python2.7/dist-packages')

The path to add can be found when running pip install <something> , (such as numpy) and it tells you where each package is installed or already exists. 运行pip install <something>时可以找到要添加的路径(例如numpy),它告诉您每个软件包的安装位置或已经存在的位置。

Is it possible that /usr/bin/python is actually a symlink? / usr / bin / python是否实际上可能是符号链接?

The better solution is that you should be running your ipython notebook from a virtualenv that contains all the modules that you need. 更好的解决方案是您应该从包含所需所有模块的virtualenv中运行ipython笔记本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM