Jupyter?notebook無法鏈接內(nèi)核、運(yùn)行代碼問題
問題來源
今天想在 vscode 上使用 Jupyter notebook 跑 Python 代碼,但無法使用,提示要升級(jí)內(nèi)核。
Running cells with base requires the ipykernel package to be installed or requires an update.
其實(shí)這個(gè)問題存在好一段時(shí)間了,不過之前沒空處理,就擱置了,今天來看看究竟是怎么回事。
解決問題過程
Jupyter 客戶端閃退-包版本沖突
vscode 上的插件不行,那就試試本地的 Jupyter notebook 客戶端,結(jié)果發(fā)現(xiàn)也不行!客戶端直接閃一下就沒有,改用命令行,發(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
安裝成功了,不過提示了很多版本沖突問題。

根據(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ò)提示來判斷)。
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,回車,終于可以啟動(dòng)。
不過打開 .ipynb 文件之后,一直提示“內(nèi)核正在啟動(dòng),請(qǐng)等待…”。

Jupyter 鏈接不上內(nèi)核,無法執(zhí)行代碼
雖然 Jupyter 客戶端打開了,但是連接不上內(nèi)核,查看命令行,發(fā)現(xiàn)幾個(gè)問題:
- 調(diào)用錯(cuò)誤
- 文件沖突
- 路徑錯(cuò)誤
- 找不到文件
問題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),這些功能包括語法高亮、多行編輯、代碼補(bǔ)全、自動(dòng)提示、使用鼠標(biāo)移動(dòng)光標(biāo)、查詢歷史以及良好的Unicode支持。
問題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 文件。
分別打開著兩個(gè)文件,發(fā)現(xiàn)二者一模一樣,而且從字面上看highlight_selected_word是高亮代碼,不影響核心功能,而且兩個(gè)配置文件都一致,選誰都無所謂,暫時(shí)不管它。
注:這個(gè) Nbextensions 是一個(gè)為 Jupyter Notebook 提供擴(kuò)展功能的庫。它包含了一系列實(shí)用的小插件,可以幫助我們實(shí)現(xiàn)文件結(jié)構(gòu)的可視化,特別適用于包含大量代碼或者需要 Markdown 混排的場(chǎng)合。

問題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
打開文件,搜索template_paths = List(['.']),在其后加上template_path = template_paths。
參考如下:
template_paths = List(['.']).tag(config=True, affects_environment=True)
template_path = template_pathsTips:或許還有另外的解決方案,這些模塊不知道配置文件中的template_path是extra_template_paths、template_name、template_paths這三中的哪一個(gè),那是不是意味著把配置文件的template_path改為template_paths便可解決呢?或許可行,不過這個(gè)問題的難點(diǎ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配置中被禁用,沒有提供一個(gè)yaml文件來告訴我們?nèi)绾闻渲盟?。但是,您仍然可以從這里啟用或禁用它。
這些文件猜測(cè)是版本變更之后,更新了拓展庫,部分拓展不再支持,所以找不到了,如果不需要的,可以挨個(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

不過可能會(huì)失敗

另外,在 echarts-maps 項(xiàng)目可以看到這些地圖的來源,不過http://localhost:8888/nbextensions/echarts-xxx.js路徑的相關(guān)文件都是 404。

Jupyter 恢復(fù)連接
經(jīng)過上面的一番折騰,客戶端和 Vscode 的 Jupyter 都已經(jīng)可以正常使用。撒花~~~
總結(jié)
好像挺長時(shí)間沒有碰家里這臺(tái)電腦的 Jupyter notebook,沒想到問題這么大,好在最終基本都修好了。
整個(gè)過程,首先是驗(yàn)證 Jupyter 出問題是 Vscode 插件的問題,還是整體都出問題,定位到時(shí)整體出問題之后,嘗試在本地打開它,結(jié)果打不開,因?yàn)椴糠职鼪_突了!解決包沖突問題,終于可以打開,不過依舊還是有問題——無法連接內(nèi)核,無法執(zhí)行代碼。根據(jù)終端報(bào)錯(cuò),逐一排查問題并解決掉:
- 調(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 shutil.move移動(dòng)文件或目錄方式
`shutil.move()`函數(shù)可以移動(dòng)文件或目錄,移動(dòng)目錄時(shí),如果目標(biāo)目錄不存在,會(huì)創(chuàng)建該目錄并將源目錄內(nèi)容移動(dòng)到新目錄;如果目標(biāo)目錄存在,則將源目錄移動(dòng)到目標(biāo)目錄下,移動(dòng)文件時(shí),如果目標(biāo)路徑是目錄,則將文件移動(dòng)到該目錄下并重命名2024-12-12
python進(jìn)行debug操作實(shí)戰(zhàn)訓(xùn)練
debug是編碼是非常重要的調(diào)試技巧,通過在運(yùn)行過程中設(shè)置斷點(diǎn),幫助開發(fā)人員更好的理解運(yùn)行過程,下面這篇文章主要給大家介紹了關(guān)于python進(jìn)行debug操作的相關(guān)資料,需要的朋友可以參考下2023-06-06
pytest解讀一次請(qǐng)求多個(gè)fixtures及多次請(qǐng)求
這篇文章主要為大家介紹了一次請(qǐng)求多個(gè)fixtures,以及fixtures被多次請(qǐng)求的pytest官方解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
python3光學(xué)字符識(shí)別模塊tesserocr與pytesseract的使用詳解
這篇文章主要介紹了python3光學(xué)字符識(shí)別模塊tesserocr與pytesseract的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
python生成每日?qǐng)?bào)表數(shù)據(jù)(Excel)并郵件發(fā)送的實(shí)例
今天小編就為大家分享一篇python生成每日?qǐng)?bào)表數(shù)據(jù)(Excel)并郵件發(fā)送的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02
使用國內(nèi)鏡像源優(yōu)化pip install下載的方法步驟
在Python開發(fā)中,pip 是一個(gè)不可或缺的工具,用于安裝和管理Python包,然而,由于默認(rèn)的PyPI服務(wù)器位于國外,國內(nèi)用戶在安裝依賴時(shí)可能會(huì)遇到下載速度慢、連接不穩(wěn)定等問題,所以本文將詳細(xì)介紹如何使用國內(nèi)鏡像源來加速pip install -r requirements.txt的過程2025-03-03

