Jupyter?notebook無(wú)法鏈接內(nèi)核、運(yùn)行代碼問(wèn)題
問(wèn)題來(lái)源
今天想在 vscode 上使用 Jupyter notebook 跑 Python 代碼,但無(wú)法使用,提示要升級(jí)內(nèi)核。
Running cells with base requires the ipykernel package to be installed or requires an update.
其實(shí)這個(gè)問(wèn)題存在好一段時(shí)間了,不過(guò)之前沒(méi)空處理,就擱置了,今天來(lái)看看究竟是怎么回事。
解決問(wèn)題過(guò)程
Jupyter 客戶端閃退-包版本沖突
vscode 上的插件不行,那就試試本地的 Jupyter notebook 客戶端,結(jié)果發(fā)現(xiàn)也不行!客戶端直接閃一下就沒(méi)有,改用命令行,發(fā)現(xiàn)一個(gè)報(bào)錯(cuò):
ImportError: The Jupyter Notebook requires tornado >= 5.0, but you have 4.5.3
安裝下tornado==5.0
。
pip install tornado==5.0
安裝成功了,不過(guò)提示了很多版本沖突問(wèn)題。
根據(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.
每安裝完一個(gè)包,會(huì)有一次紅色提示包是否沖突,有一些包會(huì)自動(dòng)補(bǔ)全安裝,最終安裝了以下 6 個(gè)(根據(jù)紅色報(bào)錯(cuò)提示來(lái)判斷)。
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,回車(chē),終于可以啟動(dòng)。
不過(guò)打開(kāi) .ipynb 文件之后,一直提示“內(nèi)核正在啟動(dòng),請(qǐng)等待…”。
Jupyter 鏈接不上內(nèi)核,無(wú)法執(zhí)行代碼
雖然 Jupyter 客戶端打開(kāi)了,但是連接不上內(nèi)核,查看命令行,發(fā)現(xiàn)幾個(gè)問(wèn)題:
- 調(diào)用錯(cuò)誤
- 文件沖突
- 路徑錯(cuò)誤
- 找不到文件
問(wèn)題1:調(diào)用錯(cuò)誤
錯(cuò)誤描述:
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)
該報(bào)錯(cuò)升級(jí)一下prompt_toolkit
即可。
pip install --upgrade prompt_toolkit
升級(jí)完是 3.0.40 版本,這時(shí)會(huì)提示另外一個(gè)報(bào)錯(cuò):
ipython 7.6.0 requires prompt-toolkit<2.1.0,>=2.0.0, but you have prompt-toolkit 3.0.40 which is incompatible.
升級(jí)ipython
,升級(jí)后版本:ipython-8.17.2,解決。
pip install --upgrade ipython
Tips:prompt_toolkit
主要用于創(chuàng)建交互式命令行和終端應(yīng)用程序。
它提供了一套豐富的功能以增強(qiáng)用戶與命令行界面的交互體驗(yàn),這些功能包括語(yǔ)法高亮、多行編輯、代碼補(bǔ)全、自動(dòng)提示、使用鼠標(biāo)移動(dòng)光標(biāo)、查詢歷史以及良好的Unicode支持。
問(wèn)題2:文件沖突
錯(cuò)誤描述:
[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’
這個(gè)是因?yàn)榇嬖趦蓚€(gè) nbextension,其目錄下各存在一個(gè) configurator.yaml 文件。
分別打開(kāi)著兩個(gè)文件,發(fā)現(xiàn)二者一模一樣,而且從字面上看highlight_selected_word
是高亮代碼,不影響核心功能,而且兩個(gè)配置文件都一致,選誰(shuí)都無(wú)所謂,暫時(shí)不管它。
注:這個(gè) Nbextensions 是一個(gè)為 Jupyter Notebook 提供擴(kuò)展功能的庫(kù)。它包含了一系列實(shí)用的小插件,可以幫助我們實(shí)現(xiàn)文件結(jié)構(gòu)的可視化,特別適用于包含大量代碼或者需要 Markdown 混排的場(chǎng)合。
問(wèn)題3:路徑錯(cuò)誤
錯(cuò)誤描述:
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”?
該報(bào)錯(cuò)是因?yàn)榕渲梦募械?code>template_path未指定相關(guān)的路徑,系統(tǒng)不清楚是哪個(gè)。
找到文件
D:\anaconda3\Lib\site-packages\nbconvert\exporters\templateexporter.py
打開(kāi)文件,搜索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
這三中的哪一個(gè),那是不是意味著把配置文件的template_path
改為template_paths
便可解決呢?或許可行,不過(guò)這個(gè)問(wèn)題的難點(diǎn)在于不知道配置文件的路徑……
問(wèn)題4:找不到文件
報(bào)錯(cuò)描述:
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
這些報(bào)錯(cuò)都是找不到相關(guān)的文件導(dǎo)致的。這些文件對(duì)應(yīng)著下面標(biāo)黃色的拓展。
隨便單擊一個(gè),可以看到一些英文提示,大意是:這些擴(kuò)展在notebook json配置中被禁用,沒(méi)有提供一個(gè)yaml文件來(lái)告訴我們?nèi)绾闻渲盟?。但是,您仍然可以從這里啟用或禁用它。
這些文件猜測(cè)是版本變更之后,更新了拓展庫(kù),部分拓展不再支持,所以找不到了,如果不需要的,可以挨個(gè)點(diǎn)一下 Forget。
如果還想要使用,可以試試 Github 上相關(guān)項(xiàng)目的安裝方案
參考 jupyter-echarts:https://github.com/pyecharts/jupyter-echarts
jupyter nbextension install echarts jupyter nbextension enable echarts/main
不過(guò)可能會(huì)失敗
另外,在 echarts-maps 項(xiàng)目可以看到這些地圖的來(lái)源,不過(guò)http://localhost:8888/nbextensions/echarts-xxx.js
路徑的相關(guān)文件都是 404。
Jupyter 恢復(fù)連接
經(jīng)過(guò)上面的一番折騰,客戶端和 Vscode 的 Jupyter 都已經(jīng)可以正常使用。撒花~~~
總結(jié)
好像挺長(zhǎng)時(shí)間沒(méi)有碰家里這臺(tái)電腦的 Jupyter notebook,沒(méi)想到問(wèn)題這么大,好在最終基本都修好了。
整個(gè)過(guò)程,首先是驗(yàn)證 Jupyter 出問(wèn)題是 Vscode 插件的問(wèn)題,還是整體都出問(wèn)題,定位到時(shí)整體出問(wèn)題之后,嘗試在本地打開(kāi)它,結(jié)果打不開(kāi),因?yàn)椴糠职鼪_突了!解決包沖突問(wèn)題,終于可以打開(kāi),不過(guò)依舊還是有問(wèn)題——無(wú)法連接內(nèi)核,無(wú)法執(zhí)行代碼。根據(jù)終端報(bào)錯(cuò),逐一排查問(wèn)題并解決掉:
- 調(diào)用錯(cuò)誤:升級(jí)一下
prompt_toolkit
,升級(jí)ipython
; - 文件沖突:忽略;
- 路徑錯(cuò)誤:修改
…\Lib\site-packages\nbconvert\exporters\templateexporter.py
文件,新增template_path = template_paths
給template_path
賦值; - 找不到文件:文件被刪,要么 Forget,要么嘗試安裝(略)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python tkinter之 復(fù)選、文本、下拉的實(shí)現(xiàn)
這篇文章主要介紹了python tkinter之 復(fù)選、文本、下拉的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03python實(shí)現(xiàn)的正則表達(dá)式功能入門(mén)教程【經(jīng)典】
這篇文章主要介紹了python實(shí)現(xiàn)的正則表達(dá)式功能,詳細(xì)分析了Python正則表達(dá)式中常用的各種符號(hào)、函數(shù)等的使用方法與注意事項(xiàng),需要的朋友可以參考下2017-06-06Python CSS選擇器爬取京東網(wǎng)商品信息過(guò)程解析
這篇文章主要介紹了Python CSS選擇器爬取京東網(wǎng)商品信息過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06pandas 數(shù)據(jù)實(shí)現(xiàn)行間計(jì)算的方法
今天小編就為大家分享一篇pandas 數(shù)據(jù)實(shí)現(xiàn)行間計(jì)算的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Python下實(shí)現(xiàn)的RSA加密/解密及簽名/驗(yàn)證功能示例
這篇文章主要介紹了Python下實(shí)現(xiàn)的RSA加密/解密及簽名/驗(yàn)證功能,結(jié)合具體實(shí)例形式分析了Python中RSA加密、解密的實(shí)現(xiàn)方法及簽名、驗(yàn)證功能的使用技巧,需要的朋友可以參考下2017-07-07