【问题标题】:Jupyter notebook is using different Kernel (python 3.8) from my default python version (Python 3.9) in my machineJupyter notebook 在我的机器中使用了与我的默认 python 版本(Python 3.9)不同的内核(python 3.8)
【发布时间】:2021-10-21 06:57:59
【问题描述】:

我正在 Jupyter Notebook 中运行一些代码,并在我的机器中发现了这个问题。

  1. 当我执行“print(sys.version)”时,我得到了我的 jupyter 内核使用的 python 版本是 3.8.2,如下所示。
  2. 当我执行“!python -V”时,我得到了另一个 python 版本,它是 3.9.1。
  3. 当我键入“!jupyter kernelspec list”时,我只有 1 个 python 3 用于 jupyter 内核。 (忽略了我为上一个项目创建的虚拟环境 TFOD)

或者可以参考下图的输出。

enter image description here

但是,在我的机器上,我只安装了两个 python 版本,分别是 3.8 和 3.9,如下所示。我使用 3.9 作为我的默认值。

enter image description here

我实际上发现代码“!python -m ipykernel install --user --name=Python_39”可以解决我的问题并让我创建另一个使用python 3.9的内核。

但我仍然热衷于为什么会发生这种情况以及如何通过适当的方式更改(或添加)与安装在我机器中的 python 相关的 Jupyter 内核?

因为 py3.9 是我的活动环境,因为我安装了许多库并希望在其上运行,而不是 jupyter notebook 中的其他 (py3.8) 版本。

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    没有提到您如何创建 Jupyter 笔记本服务器,但我建议您从 python 的 virtual environment 进行创建

    • 这应该会激活一个 python 环境,激活后,您可以一致地查看特定 python 版本和您在此环境中安装的依赖项(python 库)。
    • 当 virtualenv 被激活时,你可以使用 python 而不是 python3.x,因为激活的 virtualenv 会告诉你的 shell 在调用 python 时引用它自己的 python 版本。
    • 如果您的 jupyter 是从 virtualenv 实例化的,它将适用于这个特定的 python 版本 - 因为它是唯一知道的。
    • 如果您的项目已经使用了 venv,您可以每次都从中实例化 jupyter。

    这是我在 linux shell 上做的一个 sn-p:

    // create a new venv:
    shell# python3.9 -m venv ./myenv/
    // activate the venv to operate on the shell:
    shell# source ./myenv/bin/activate 
    // install jupyter to the venvs libraries
    (myvenv) shell# python3.9 -m pip install jupyter
    // run the jupyter server
    (myvenv) shell# python -m jupyter notebook --port=8888 --no-browser --ip=0.0.0.0 --allow-root"
    

    【讨论】:

    • 您好,感谢您的回复,这让我想起了一些事情。我认为我的问题是因为我上次使用 python3.8 时安装了 jupyter notebook。虽然我已经将路径转移到 python 3.9 并将其用作我的默认 python,但是当我启动我的笔记本时,它仍将使用在 3.8 期间安装的内核。我已经通过使用我的 py3.9 再次运行 pip install jupyter 来验证它,而 jupyternotebook 现在正在使用 3.9 的内核!将在我即将到来的项目中更多地练习虚拟环境,感谢您的洞察力!
    • 干杯。如果有帮助,请考虑对答案进行投票:)
    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多