如何在运行juptyer笔记本时修复导入错误?

时间:2017-10-05 07:00:30

标签: python jupyter

我有一个python jupyter笔记本,我试图在命令行上执行。这是命令:

jupyter nbconvert --to notebook --execute --ExecutePreprocessor.timeout=60 --output out_file test.ipynb

这是此笔记本的测试版(test.ipynb

{"cells":[{"metadata":{"deletable":true,"editable":true},"cell_type":"markdown","source":["# Morphology analysis with neurom\n","\n","You can find the full documentation of neurom on http://neurom.readthedocs.io/en/latest/index.html ."]},{"metadata":{"collapsed":false,"deletable":true,"editable":true,"trusted":false},"cell_type":"code","source":["%matplotlib inline\n","from copy import deepcopy\n","\n","from IPython.display import display, HTML\n","import urllib, zipfile, os\n","\n","try:\n","    import neurom\n","    from neurom import viewer, stats\n","except ImportError:\n","    !pip2 install neurom\n","    import neurom\n","    from neurom import viewer, stats"],"execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"python2","display_name":"Python 2","language":"python"},"language_info":{"version":"2.7.6","mimetype":"text/x-python","file_extension":".py","codemirror_mode":{"version":2,"name":"ipython"},"nbconvert_exporter":"python","name":"python","pygments_lexer":"ipython2"}},"nbformat":4,"nbformat_minor":2}

对应于以下代码:

get_ipython().magic(u'matplotlib inline')
from copy import deepcopy

from IPython.display import display, HTML
import urllib, zipfile, os

try:
    import neurom
    from neurom import viewer, stats
except ImportError:
    get_ipython().system(u'pip2 install neurom')
    import neurom
    from neurom import viewer, stats

使用上面的命令运行此代码,我收到以下错误

ImportError: No module named neurom

虽然此模块安装在当前设置中。那么为什么python没有拿起这个模块呢?

1 个答案:

答案 0 :(得分:0)

问题似乎是笔记本电脑的执行不使用当前环境(例如virtualenv),您必须将其“添加”到jupyter笔记本中。

首先,使用此命令生成标准配置

jupyter notebook --generate-config

通常生成文件~/.jupyter/jupyter_notebook_config.py。在该文件中,您必须添加以下部分

c.InteractiveShellApp.exec_lines=[
    'import sys; sys.path.append("path-to-add")'
]

将路径添加到执行中。

另见线程here