Jupyter?notebook無法鏈接內(nèi)核、運行代碼問題
問題來源
今天想在 vscode 上使用 Jupyter notebook 跑 Python 代碼,但無法使用,提示要升級內(nèi)核。
Running cells with base requires the ipykernel package to be installed or requires an update.
其實這個問題存在好一段時間了,不過之前沒空處理,就擱置了,今天來看看究竟是怎么回事。
解決問題過程
Jupyter 客戶端閃退-包版本沖突
vscode 上的插件不行,那就試試本地的 Jupyter notebook 客戶端,結(jié)果發(fā)現(xiàn)也不行!客戶端直接閃一下就沒有,改用命令行,發(fā)現(xiàn)一個報錯:
ImportError: The Jupyter Notebook requires tornado >= 5.0, but you have 4.5.3
安裝下tornado==5.0
。
pip install tornado==5.0
安裝成功了,不過提示了很多版本沖突問題。
根據(jù)紅色提示,逐一安裝相關(guān)的依賴包。
anaconda-project 0.10.2 requires ruamel-yaml, which is not installed.
spyder 5.1.5 requires pyqt5<5.13, which is not installed.
spyder 5.1.5 requires pyqtwebengine<5.13, which is not installed.
bokeh 2.4.2 requires tornado>=5.1, but you have tornado 5.0 which is incompatible.
distributed 2022.2.1 requires tornado>=6.0.3, but you have tornado 5.0 which is incompatible.
jupyter-console 6.4.0 requires prompt-toolkit!=3.0.0,!=3.0.1,< 3.1.0,>=2.0.0, but you have prompt-toolkit 1.0.18 which is incompatible.
jupyter-server 1.13.5 requires pywinpty<2; os_name == “nt”, but you have pywinpty 2.0.2 which is incompatible.
jupyter-server 1.13.5 requires tornado>=6.1.0, but you have tornado 5.0 which is incompatible.
jupyterlab 3.3.2 requires tornado>=6.1.0, but you have tornado 5.0 which is incompatible.
notebook 6.4.8 requires tornado>=6.1, but you have tornado 5.0 which is incompatible.
spyder 5.1.5 requires** ipython>=7.6.0**, but you have ipython 5.5.0 which is incompatible.
spyder-kernels 2.1.3 requires ipykernel>=5.3.0; python_version >= “3”, but you have ipykernel 4.6.1 which is incompatible.
spyder-kernels 2.1.3 requires ipython>=7.6.0; python_version >= “3”, but you have ipython 5.5.0 which is incompatible.
streamlit 1.16.0 requires protobuf<4,>=3.12, but you have protobuf 4.21.12 which is incompatible.
每安裝完一個包,會有一次紅色提示包是否沖突,有一些包會自動補全安裝,最終安裝了以下 6 個(根據(jù)紅色報錯提示來判斷)。
pip install tornado==6.1.0 pip install ipython==7.6.0 pip install ipykernel==5.3.0 pip install prompt-toolkit==3.0.2 pip install pyqt5==5.12.3 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install pyqtwebengine==5.12.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
安裝完了,在命令行再次輸入 jupyter notebook,回車,終于可以啟動。
不過打開 .ipynb 文件之后,一直提示“內(nèi)核正在啟動,請等待…”。
Jupyter 鏈接不上內(nèi)核,無法執(zhí)行代碼
雖然 Jupyter 客戶端打開了,但是連接不上內(nèi)核,查看命令行,發(fā)現(xiàn)幾個問題:
- 調(diào)用錯誤
- 文件沖突
- 路徑錯誤
- 找不到文件
問題1:調(diào)用錯誤
錯誤描述:
ImportError: cannot import name ‘to_formatted_text’ from partially initialized module ‘prompt_toolkit.formatted_text’ (most likely due to a circular import) (D:\anaconda3\Lib\site-packages\prompt_toolkit\formatted_text_init_.py)
該報錯升級一下prompt_toolkit
即可。
pip install --upgrade prompt_toolkit
升級完是 3.0.40 版本,這時會提示另外一個報錯:
ipython 7.6.0 requires prompt-toolkit<2.1.0,>=2.0.0, but you have prompt-toolkit 3.0.40 which is incompatible.
升級ipython
,升級后版本:ipython-8.17.2,解決。
pip install --upgrade ipython
Tips:prompt_toolkit
主要用于創(chuàng)建交互式命令行和終端應用程序。
它提供了一套豐富的功能以增強用戶與命令行界面的交互體驗,這些功能包括語法高亮、多行編輯、代碼補全、自動提示、使用鼠標移動光標、查詢歷史以及良好的Unicode支持。
問題2:文件沖突
錯誤描述:
[jupyter_nbextensions_configurator] nbextension ‘highlight_selected_word/main’ has duplicate listings in both ‘C:\ProgramData\jupyter\nbextensions\highlight_selected_word\configurator.yaml’ and ‘D:\anaconda3\share\jupyter\nbextensions\highlight_selected_word\configurator.yaml’
這個是因為存在兩個 nbextension,其目錄下各存在一個 configurator.yaml 文件。
分別打開著兩個文件,發(fā)現(xiàn)二者一模一樣,而且從字面上看highlight_selected_word
是高亮代碼,不影響核心功能,而且兩個配置文件都一致,選誰都無所謂,暫時不管它。
注:這個 Nbextensions 是一個為 Jupyter Notebook 提供擴展功能的庫。它包含了一系列實用的小插件,可以幫助我們實現(xiàn)文件結(jié)構(gòu)的可視化,特別適用于包含大量代碼或者需要 Markdown 混排的場合。
問題3:路徑錯誤
錯誤描述:
Config option “template_path” not recognized by “ExporterCollapsibleHeadings”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “TocExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsHTMLExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsTocHTMLExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsLatexExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsSlidesExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “ExporterCollapsibleHeadings”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “TocExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsHTMLExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsTocHTMLExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsLatexExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
Config option “template_path” not recognized by “LenvsSlidesExporter”. Did you mean one of: “extra_template_paths, template_name, template_paths”?
該報錯是因為配置文件中的template_path
未指定相關(guān)的路徑,系統(tǒng)不清楚是哪個。
找到文件
D:\anaconda3\Lib\site-packages\nbconvert\exporters\templateexporter.py
打開文件,搜索template_paths = List(['.'])
,在其后加上template_path = template_paths
。
參考如下:
template_paths = List(['.']).tag(config=True, affects_environment=True) template_path = template_paths
Tips:或許還有另外的解決方案,這些模塊不知道配置文件中的template_path
是extra_template_paths
、template_name
、template_paths
這三中的哪一個,那是不是意味著把配置文件的template_path
改為template_paths
便可解決呢?或許可行,不過這個問題的難點在于不知道配置文件的路徑……
問題4:找不到文件
報錯描述:
404 GET /nbextensions/pydeck/extension.js?v=20231111174515 (::1) 21.940000ms referer=http://localhost:8888/notebooks/test_df.ipynb
404 GET /nbextensions/echarts-countries-js/index.js?v=20231111174515 (::1) 14.600000ms referer=http://localhost:8888/notebooks/test_df.ipynb
404 GET /nbextensions/echarts-china-provinces-js/main.js?v=20231111174515 (::1) 18.590000ms referer=http://localhost:8888/notebooks/test_df.ipynb
404 GET /nbextensions/echarts-china-cities-js/index.js?v=20231111174515 (::1) 7.980000ms referer=http://localhost:8888/notebooks/test_df.ipynb
404 GET /nbextensions/echarts-china-misc-js/index.js?v=20231111174515 (::1) 15.960000ms referer=http://localhost:8888/notebooks/test_df.ipynb
404 GET /nbextensions/echarts-united-kingdom-js/main.js?v=20231111174515 (::1) 15.960000ms referer=http://localhost:8888/notebooks/test_df.ipynb
404 GET /nbextensions/echarts-themes-js/index.js?v=20231111174515 (::1) 20.940000ms referer=http://localhost:8888/notebooks/test_df.ipynb
Kernel started: 4b184439-6a4f-4ea9-bc64-3a4a3cb586e0, name: python3
404 GET /nbextensions/widgets/notebook/js/extension.js?v=20231111174515 (::1) 21.940000ms referer=http://localhost:8888/notebooks/test_df.ipynb
這些報錯都是找不到相關(guān)的文件導致的。這些文件對應著下面標黃色的拓展。
隨便單擊一個,可以看到一些英文提示,大意是:這些擴展在notebook json配置中被禁用,沒有提供一個yaml文件來告訴我們?nèi)绾闻渲盟?。但是,您仍然可以從這里啟用或禁用它。
這些文件猜測是版本變更之后,更新了拓展庫,部分拓展不再支持,所以找不到了,如果不需要的,可以挨個點一下 Forget。
如果還想要使用,可以試試 Github 上相關(guān)項目的安裝方案
參考 jupyter-echarts:https://github.com/pyecharts/jupyter-echarts
jupyter nbextension install echarts jupyter nbextension enable echarts/main
不過可能會失敗
另外,在 echarts-maps 項目可以看到這些地圖的來源,不過http://localhost:8888/nbextensions/echarts-xxx.js
路徑的相關(guān)文件都是 404。
Jupyter 恢復連接
經(jīng)過上面的一番折騰,客戶端和 Vscode 的 Jupyter 都已經(jīng)可以正常使用。撒花~~~
總結(jié)
好像挺長時間沒有碰家里這臺電腦的 Jupyter notebook,沒想到問題這么大,好在最終基本都修好了。
整個過程,首先是驗證 Jupyter 出問題是 Vscode 插件的問題,還是整體都出問題,定位到時整體出問題之后,嘗試在本地打開它,結(jié)果打不開,因為部分包沖突了!解決包沖突問題,終于可以打開,不過依舊還是有問題——無法連接內(nèi)核,無法執(zhí)行代碼。根據(jù)終端報錯,逐一排查問題并解決掉:
- 調(diào)用錯誤:升級一下
prompt_toolkit
,升級ipython
; - 文件沖突:忽略;
- 路徑錯誤:修改
…\Lib\site-packages\nbconvert\exporters\templateexporter.py
文件,新增template_path = template_paths
給template_path
賦值; - 找不到文件:文件被刪,要么 Forget,要么嘗試安裝(略)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python tkinter之 復選、文本、下拉的實現(xiàn)
這篇文章主要介紹了python tkinter之 復選、文本、下拉的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python實現(xiàn)的正則表達式功能入門教程【經(jīng)典】
這篇文章主要介紹了python實現(xiàn)的正則表達式功能,詳細分析了Python正則表達式中常用的各種符號、函數(shù)等的使用方法與注意事項,需要的朋友可以參考下2017-06-06Python CSS選擇器爬取京東網(wǎng)商品信息過程解析
這篇文章主要介紹了Python CSS選擇器爬取京東網(wǎng)商品信息過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06pandas 數(shù)據(jù)實現(xiàn)行間計算的方法
今天小編就為大家分享一篇pandas 數(shù)據(jù)實現(xiàn)行間計算的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python下實現(xiàn)的RSA加密/解密及簽名/驗證功能示例
這篇文章主要介紹了Python下實現(xiàn)的RSA加密/解密及簽名/驗證功能,結(jié)合具體實例形式分析了Python中RSA加密、解密的實現(xiàn)方法及簽名、驗證功能的使用技巧,需要的朋友可以參考下2017-07-07