最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Jupyter?notebook無法鏈接內核、運行代碼問題

 更新時間:2025年02月20日 17:04:31   作者:Xin學數(shù)據(jù)  
文章主要介紹了在VSCode中使用Jupyter?Notebook遇到的問題及其解決過程,問題包括包版本沖突、文件沖突、路徑錯誤和找不到文件,通過逐一排查和安裝相關依賴包,最終解決了這些問題,使得Jupyter?Notebook可以在VSCode中正常運行

問題來源

今天想在 vscode 上使用 Jupyter notebook 跑 Python 代碼,但無法使用,提示要升級內核。

Running cells with base requires the ipykernel package to be installed or requires an update.

其實這個問題存在好一段時間了,不過之前沒空處理,就擱置了,今天來看看究竟是怎么回事。

解決問題過程

Jupyter 客戶端閃退-包版本沖突

vscode 上的插件不行,那就試試本地的 Jupyter notebook 客戶端,結果發(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ù)紅色提示,逐一安裝相關的依賴包。

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 文件之后,一直提示“內核正在啟動,請等待…”。

Jupyter 鏈接不上內核,無法執(zhí)行代碼

雖然 Jupyter 客戶端打開了,但是連接不上內核,查看命令行,發(fā)現(xiàn)幾個問題:

  • 調用錯誤
  • 文件沖突
  • 路徑錯誤
  • 找不到文件

問題1:調用錯誤

錯誤描述:

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)文件結構的可視化,特別適用于包含大量代碼或者需要 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未指定相關的路徑,系統(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_pathextra_template_paths、template_nametemplate_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

這些報錯都是找不到相關的文件導致的。這些文件對應著下面標黃色的拓展。

隨便單擊一個,可以看到一些英文提示,大意是:這些擴展在notebook json配置中被禁用,沒有提供一個yaml文件來告訴我們如何配置它。但是,您仍然可以從這里啟用或禁用它。

這些文件猜測是版本變更之后,更新了拓展庫,部分拓展不再支持,所以找不到了,如果不需要的,可以挨個點一下 Forget。

如果還想要使用,可以試試 Github 上相關項目的安裝方案

參考 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路徑的相關文件都是 404。

Jupyter 恢復連接

經過上面的一番折騰,客戶端和 Vscode 的 Jupyter 都已經可以正常使用。撒花~~~

總結

好像挺長時間沒有碰家里這臺電腦的 Jupyter notebook,沒想到問題這么大,好在最終基本都修好了。

整個過程,首先是驗證 Jupyter 出問題是 Vscode 插件的問題,還是整體都出問題,定位到時整體出問題之后,嘗試在本地打開它,結果打不開,因為部分包沖突了!解決包沖突問題,終于可以打開,不過依舊還是有問題——無法連接內核,無法執(zhí)行代碼。根據(jù)終端報錯,逐一排查問題并解決掉:

  • 調用錯誤:升級一下prompt_toolkit,升級ipython
  • 文件沖突:忽略;
  • 路徑錯誤:修改…\Lib\site-packages\nbconvert\exporters\templateexporter.py文件,新增template_path = template_pathstemplate_path賦值;
  • 找不到文件:文件被刪,要么 Forget,要么嘗試安裝(略)。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python shutil.move移動文件或目錄方式

    python shutil.move移動文件或目錄方式

    `shutil.move()`函數(shù)可以移動文件或目錄,移動目錄時,如果目標目錄不存在,會創(chuàng)建該目錄并將源目錄內容移動到新目錄;如果目標目錄存在,則將源目錄移動到目標目錄下,移動文件時,如果目標路徑是目錄,則將文件移動到該目錄下并重命名
    2024-12-12
  • python進行debug操作實戰(zhàn)訓練

    python進行debug操作實戰(zhàn)訓練

    debug是編碼是非常重要的調試技巧,通過在運行過程中設置斷點,幫助開發(fā)人員更好的理解運行過程,下面這篇文章主要給大家介紹了關于python進行debug操作的相關資料,需要的朋友可以參考下
    2023-06-06
  • python快速編寫單行注釋多行注釋的方法

    python快速編寫單行注釋多行注釋的方法

    在本篇文章里小編給大家整理的是關于python快速編寫單行注釋多行注釋的方法,有此需要的朋友們可以參考學習下。
    2019-07-07
  • Python中位運算的詳細用法教程

    Python中位運算的詳細用法教程

    在Python中,位運算是一種對二進制數(shù)進行操作的運算方式,它們直接對二進制位進行操作,而不考慮這些位所表示的實際值,本文將詳細介紹Python中的位運算符,需要的朋友可以參考下
    2024-08-08
  • pytest解讀一次請求多個fixtures及多次請求

    pytest解讀一次請求多個fixtures及多次請求

    這篇文章主要為大家介紹了一次請求多個fixtures,以及fixtures被多次請求的pytest官方解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • python3光學字符識別模塊tesserocr與pytesseract的使用詳解

    python3光學字符識別模塊tesserocr與pytesseract的使用詳解

    這篇文章主要介紹了python3光學字符識別模塊tesserocr與pytesseract的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 詳解Django中的form庫的使用

    詳解Django中的form庫的使用

    這篇文章主要介紹了詳解Django中的form庫的使用,Django是最為著名的Python編程框架,需要的朋友可以參考下
    2015-07-07
  • python生成每日報表數(shù)據(jù)(Excel)并郵件發(fā)送的實例

    python生成每日報表數(shù)據(jù)(Excel)并郵件發(fā)送的實例

    今天小編就為大家分享一篇python生成每日報表數(shù)據(jù)(Excel)并郵件發(fā)送的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python中的嵌套循環(huán)詳情

    Python中的嵌套循環(huán)詳情

    這篇文章主要介紹了Python中的嵌套循環(huán)詳情,嵌套循環(huán)就是一個外循環(huán)的主體部分是一個內循環(huán)。內循環(huán)或外循環(huán)可以是任何類型,下列詳情介紹內容,需要的朋友可以參考一下
    2022-03-03
  • 使用國內鏡像源優(yōu)化pip install下載的方法步驟

    使用國內鏡像源優(yōu)化pip install下載的方法步驟

    在Python開發(fā)中,pip 是一個不可或缺的工具,用于安裝和管理Python包,然而,由于默認的PyPI服務器位于國外,國內用戶在安裝依賴時可能會遇到下載速度慢、連接不穩(wěn)定等問題,所以本文將詳細介紹如何使用國內鏡像源來加速pip install -r requirements.txt的過程
    2025-03-03

最新評論

奉贤区| 溆浦县| 苏尼特右旗| 万全县| 玛沁县| 新密市| 保定市| 襄樊市| 彭泽县| 茌平县| 内黄县| 綦江县| 弥勒县| 贡嘎县| 商南县| 新巴尔虎左旗| 晋州市| 嘉荫县| 南漳县| 获嘉县| 吉水县| 堆龙德庆县| 北川| 平阴县| 汶上县| 河源市| 湖口县| 林甸县| 天津市| 石景山区| 鸡西市| 阿拉善左旗| 扬中市| 景东| 唐山市| 安岳县| 洪泽县| 中宁县| 龙海市| 北京市| 台山市|