问题
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'
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)
Edit: I am using a mac and not Windows or linux.
Edit: 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.
回答1:
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
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'
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.
This location is under the same path as - '/Users/User/Library/Jupyter/kernels'
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
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"
}
Next was adding another interpreter to the jupyter notebook
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 -
- create a new folder in
'/Users/User/Library/Jupyter/kernels'
- copy the above
kernel.json
inside it - 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"
}
Once done, you can check your available kernels using -
!jupyter kernelspec list
Available kernels:
python3 /Users/User/Library/Jupyter/kernels/python3
tf_mac /Users/User/Library/Jupyter/kernels/tf_mac
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.
来源:https://stackoverflow.com/questions/64949574/jupyter-interpreter-permanently-changed