2017-10-05 81 views
0

我有一個python jupyter筆記本,我試圖在命令行上執行。下面是一個命令:運行juptyer筆記本時如何修復導入錯誤?

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爲什麼不拿起這個模塊呢?

回答

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

相關問題