Jupyter notebook 使用的内核 (python 3.8) 与我机器中的默认 python 版本 (Python 3.9) 不同
Jupyter notebook is using different Kernel (python 3.8) from my default python version (Python 3.9) in my machine
我正在 运行在 Jupyter Notebook 中编写一些代码,并在我的机器中发现了这个问题。
- 当我执行“print(sys.version)”时,我得到了我的 python 版本,我的 jupyter 内核使用的是 3.8.2,如下所示。
- 当我执行“!python -V”时,我得到了另一个 python 版本,即 3.9.1。
- 当我输入“!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”可以解决我的问题,让我创建了另一个使用[=41=的内核] 3.9.
但我仍然很想知道为什么会发生这种情况,以及如何通过适当的方式更改(或添加)与安装在我机器中的 python 相对应的 Jupyter 内核?
因为 py3.9 是我的活动环境,因为我安装了很多库并希望在其上 运行 而不是 jupyter notebook 中的其他 (py3.8) 版本。
没有提到如何创建 Jupyter notebook 服务器,但我建议您从 python 的 virtual environment
开始
- 这应该会激活一个 python 的环境,激活后,您可以看到特定 python 版本和您在此环境中安装的依赖项(python 库)的一致视图.
- 当 virtualenv 被激活时,你可以只使用
python
而不是 python3.x
因为激活的 virtualenv 告诉你的 shell 引用它自己的 python 版本当调用 python
时。
- 如果你的 jupyter 是从 virtualenv 实例化的,它将与这个特定的 python 版本一起工作——因为它是唯一知道的版本。
- 如果您的项目已经使用了 venv,您每次都可以从中实例化 jupyter。
这是我在 linux shell:
上所做的片段
// 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"
- 您在笔记本中看到的 workspace\directories 将是您实例化它的那个。
- 您可以从浏览器访问 jupyter notebook 的用户界面:http://127.0.0.1:8888
- 您也可以尝试这篇文章或类似文章:https://www.codingforentrepreneurs.com/blog/install-jupyter-notebooks-virtualenv/
我正在 运行在 Jupyter Notebook 中编写一些代码,并在我的机器中发现了这个问题。
- 当我执行“print(sys.version)”时,我得到了我的 python 版本,我的 jupyter 内核使用的是 3.8.2,如下所示。
- 当我执行“!python -V”时,我得到了另一个 python 版本,即 3.9.1。
- 当我输入“!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”可以解决我的问题,让我创建了另一个使用[=41=的内核] 3.9.
但我仍然很想知道为什么会发生这种情况,以及如何通过适当的方式更改(或添加)与安装在我机器中的 python 相对应的 Jupyter 内核?
因为 py3.9 是我的活动环境,因为我安装了很多库并希望在其上 运行 而不是 jupyter notebook 中的其他 (py3.8) 版本。
没有提到如何创建 Jupyter notebook 服务器,但我建议您从 python 的 virtual environment
开始- 这应该会激活一个 python 的环境,激活后,您可以看到特定 python 版本和您在此环境中安装的依赖项(python 库)的一致视图.
- 当 virtualenv 被激活时,你可以只使用
python
而不是python3.x
因为激活的 virtualenv 告诉你的 shell 引用它自己的 python 版本当调用python
时。 - 如果你的 jupyter 是从 virtualenv 实例化的,它将与这个特定的 python 版本一起工作——因为它是唯一知道的版本。
- 如果您的项目已经使用了 venv,您每次都可以从中实例化 jupyter。
这是我在 linux shell:
上所做的片段// 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"
- 您在笔记本中看到的 workspace\directories 将是您实例化它的那个。
- 您可以从浏览器访问 jupyter notebook 的用户界面:http://127.0.0.1:8888
- 您也可以尝试这篇文章或类似文章:https://www.codingforentrepreneurs.com/blog/install-jupyter-notebooks-virtualenv/