Homebrew安装的Anaconda中JupyterLab无法启动内核,仍查找旧路径
Hey there, let’s work through this kernel problem you’re facing. It sounds like even after reinstalling Anaconda via Homebrew Cask, Jupyter Lab is still clinging to the old installation path—here’s how to wipe that out and get things working again:
Step 1: Identify Where Jupyter Is Looking for Kernels
First, let’s list all the kernels Jupyter knows about to confirm the old path is still hanging around. Run this in your terminal:
jupyter kernelspec list
You’ll see output like this (keep an eye out for entries pointing to $HOME/anaconda):
Available kernels:
python3 /Users/yourname/Library/Jupyter/kernels/python3
old_env /Users/yourname/anaconda/share/jupyter/kernels/old_env
Step 2: Update the Kernel Configuration File
For the problematic kernel (usually python3), navigate to its directory and edit the kernel.json file. Let’s use the python3 example:
open /Users/yourname/Library/Jupyter/kernels/python3/kernel.json
In the file, find the argv line—it’ll have the old Anaconda path. Replace that with your new Homebrew-installed Anaconda Python path. For example, change:
"argv": [ "/Users/yourname/anaconda/bin/python", "-m", "ipykernel_launcher", "-f", "{connection_file}" ]
To:
"argv": [ "/usr/local/anaconda3/bin/python", "-m", "ipykernel_launcher", "-f", "{connection_file}" ]
Save the file and close it.
Step 3: Ensure Your Environment Variables Are Correct and Active
Double-check that your PATH configuration is loaded properly. If you added the line to .bashrc, run this to refresh your terminal session:
source ~/.bashrc
Note: If you’re using Zsh (even though High Sierra defaults to Bash, some users switch), move the PATH line to .zshrc instead—Zsh doesn’t read .bashrc by default.
Verify the path is set correctly by running:
echo $PATH
You should see /usr/local/anaconda3/bin near the start of the output.
Step 4: Re-Register the Kernel with Your New Anaconda
Sometimes the kernel registration gets stuck on the old environment. Let’s re-install the ipykernel and register it properly:
First, activate your base conda environment:
conda activate base
Then run:
conda install ipykernel --force-reinstall python -m ipykernel install --user --name base --display-name "Python (base)"
This will overwrite the existing kernel entry with your new base environment.
Step 5: Remove Any Leftover Old Kernels
If you still see old kernel entries from the jupyter kernelspec list command, delete them with:
jupyter kernelspec remove <old-kernel-name>
Replace <old-kernel-name> with the name of the outdated kernel (like the one pointing to $HOME/anaconda).
Final Check
Close all terminal windows and restart Jupyter Lab. When you create a new notebook, select the Python (base) kernel (or the one you just registered)—it should now use the correct Anaconda path and start successfully.
内容的提问来源于stack exchange,提问作者BobTodd

