Jupyter解释器永久更改 [英] Jupyter interpreter permanently changed

查看:508
本文介绍了Jupyter解释器永久更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩一个新库,该库在以下位置设置了新的venv和解释器(python 3.8).

I was playing around with a new library which setup a new venv and interpreter (python 3.8) to the following location.

import sys
sys.executable

'/Users/User/tensorflow_macos_venv/bin/python'

虽然我激活了venv很好,但是一旦停用它并再次运行Anaconda3 jupyter笔记本,我仍然看到相同的解释器.有人可以帮忙解释一下如何将其还原为原始格式吗?我尝试转到更改内核"选项(如此答案中所述),但我只看到一个选项"python 3" ;在那里.

While this was fine when I had activated the venv, once I deactivate it and run the Anaconda3 jupyter notebook again, I still see the same interpreter. Could someone help explain how to can I revert it back to the original? I tried going to the "change kernel" option (as mentioned in this answer) but I only see one option "python 3" there.

为了澄清,在终端中它仍然显示默认解释器是正确的-

Just to clarify, in the terminal it still shows the default interpreter to be the correct one -

$which python
$/Users/User/opt/anaconda3/bin/python

更重要的是,有没有一种方法可以让我通过笔记本选项同时使用这两个选项(在更改内核选项中)

More importantly, is there a way I can have both available through the notebook options as it should be (in change kernel option)

我使用的是Mac,而不是Windows或Linux. 似乎我已经用新的venv解释器路径覆盖了Python 3内核的解释器路径.我已经在下面提到了要解决此问题的步骤.

I am using a mac and not Windows or linux. It seems I had overwritten the interpreter path for the Python 3 kernel with the new venv interpreter path. I have mentioned the steps I have followed to fix this below.

推荐答案

我花了一些时间进行研究,并找到了解决此问题并向笔记本添加多个内核的方法.我不确定这是否是最好的方法,但是,这可能会帮助其他面临此问题的人.

I have spent some time researching and found a way to fix the issue and add multiple kernels to the notebook. I am not sure if this is the best way to do it, however, this could help others facing the issue.

$which python
$/Users/User/opt/anaconda3/bin/python

以上命令显示了系统的默认解释器,但是,Jupyter Notebook从运行时读取的connection file获取其选定的解释器.可以在这里找到它,但是文件本身并不重要-

The above command shows the default interpreter of the system however, Jupyter notebook gets its selected interpreter from a connection file which it reads during runtime. Its can be found here but the file itself is not important -

from jupyter_client import find_connection_file
find_connection_file()

'/Users/User/Library/Jupyter/runtime/kernel-b1fa5520-5ae5-431a-8768-4ab4b755829f.json'

重要的是位置,因为此连接文件引用了可用的kernel.json文件,该文件实际上存储有关jupyter Notebook可使用的解释器的详细信息.

What is important is the location, since this connection file refers to the available kernel.json files which actually store the details about the interpreters available to jupyter notebook.

此位置与-'/Users/User/Library/Jupyter/kernels'

在这里,您将找到每个可用解释器的文件夹(默认情况下只有一个文件夹).在文件夹(通常称为python3)内,您会找到kernel.json

Here you will find a folder for each available interpreter (by default only a single folder). Inside the folder (usually called python3), you will find the kernel.json

就我而言,它应该如下所示.但是,通往我的道路不正确.我将其更改回默认系统python的路径,如下所示,它修复了我返回原始解释器的第一个问题-

In my case, it should have looked like below. However, the path for mine was incorrect. I changed it back to the path to my default system python as below and it fixed my first issue of getting back to the original interpreter -

{
 "argv": [
  "/Users/User/opt/anaconda3/bin/python",  #<------ fixed this
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

下一步是在jupyter笔记本中添加另一个解释器

如果要在内核选择中添加另一个解释器(菜单>内核>更改内核),那么您要做的就是-

If you have another interpreter which you want to add to the selection of kernels (Menu > Kernel > Change Kernel), then all you need to do is -

  1. '/Users/User/Library/Jupyter/kernels'
  2. 中创建一个新文件夹
  3. 将上面的kernel.json复制到其中
  4. 修改路径和显示名称
  1. create a new folder in '/Users/User/Library/Jupyter/kernels'
  2. copy the above kernel.json inside it
  3. modify the path and the display name

就我而言,我做了以下事情-

In my case, I made the following -

{
 "argv": [
  "/Users/User/tensorflow_macos_venv/bin/python",  #<---- changed this
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "tf_mac",  #<---- changed this
 "language": "python"
}

完成后,您可以使用-

!jupyter kernelspec list

Available kernels:
  python3    /Users/User/Library/Jupyter/kernels/python3
  tf_mac     /Users/User/Library/Jupyter/kernels/tf_mac

这意味着现在您可以进入jupyter笔记本菜单Kernels>更改内核并找到上述2个内核即可在两个内核之间进行实时切换.

This means that now you can go into the jupyter notebook menu Kernels > Change Kernels and find the above 2 kernels to switch between on the fly.

这就是我解决上述问题的方法.对更好的方法持开放态度,因为我敢肯定会有.

That's how I solved the above issue. Open to better methods because I am sure there would be.

这篇关于Jupyter解释器永久更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆