Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one connect a Jupyter Kernel to VS Code if one does not connect automatically?

I want to be able to have a working jupyter notebook working for VS code out of the box, with minimal work on my side.

I tried opening a jupyter notebook. I immediately found the interpreter and used my conda env with the command pellet (command + shift + P then in the drop down menu found my conda env). This seems to make the terminal work since which python points to the right place:

(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ which python
/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python

also running python scripts from VS Code seems to be working fine too, see output:

(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ /Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python /Users/brandomiranda/automl-meta-learning/python_playground.py

x = 1
my_str = this is a string
y = 2

but when I try the jupyter notebook it doesn't work.

Most noticeably my VS code does not have a kernel connected, look at the screenshot:

enter image description here

I tried clicking on a couple of arrows as suggested on reddit (vs_code_jupyter_server_no_kernel_python_not):

enter image description here

When I try running things in my jupyter notebook I get the following error:

Error: Activating Python 3.7.6 64-bit ('base': conda) to run Jupyter failed with Error: StdErr from ShellExec, /Users/brandomiranda/.bashrc: line 31: jump-module.bash: No such file or directory

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init 

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


.

which is strange. My shell is not even bash so I don't know why that message is being shown to me (maybe VS Code doesn't know Mac OS Catalina uses zsh? not sure how to fix that for VS Code). Regardless, I proceeded to do what I think the error message is suggesting. So I did the following:

I ran conda init and then went down to the terminal inside vs code and restarted the shell by running zsh but the jupyter kernel still doesn't work on my notebook in vs code. Not sure what's wrong.

In addition, I did what the error message suggested:

(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ conda init zsh
no change     /Users/brandomiranda/miniconda3/condabin/conda
no change     /Users/brandomiranda/miniconda3/bin/conda
no change     /Users/brandomiranda/miniconda3/bin/conda-env
no change     /Users/brandomiranda/miniconda3/bin/activate
no change     /Users/brandomiranda/miniconda3/bin/deactivate
no change     /Users/brandomiranda/miniconda3/etc/profile.d/conda.sh
no change     /Users/brandomiranda/miniconda3/etc/fish/conf.d/conda.fish
no change     /Users/brandomiranda/miniconda3/shell/condabin/Conda.psm1
no change     /Users/brandomiranda/miniconda3/shell/condabin/conda-hook.ps1
no change     /Users/brandomiranda/miniconda3/lib/python3.7/site-packages/xontrib/conda.xsh
no change     /Users/brandomiranda/miniconda3/etc/profile.d/conda.csh
no change     /Users/brandomiranda/.zshrc
No action taken.

but it seems it made no difference.

Any idea how to fix this?


I was suggessted by the developers of the VS code python extension to follow whatever they did here:

https://github.com/microsoft/vscode-python/issues/9566

but I can't figure out exactly it is they want me to do.

How do I fix this?


Related resources:

  • reddit: https://www.reddit.com/r/vscode/comments/eq2bfv/vs_code_jupyter_server_no_kernel_python_not/
  • gitissue: https://github.com/microsoft/vscode-python/issues/9636
  • I initially thought it was a conda issue so I looked into this question: Activate conda environment stopped working in vscode
  • https://www.reddit.com/r/vscode/comments/eshxka/how_does_one_connect_a_jupyter_kernel_to_vs_code/
  • https://www.quora.com/unanswered/How-does-one-connect-a-Jupyter-Kernel-to-VS-Code-if-one-does-not-connect-automatically
like image 775
Charlie Parker Avatar asked Jan 22 '20 20:01

Charlie Parker


People also ask

Why is my Jupyter Notebook not connecting to kernel?

A kernel error occurs basically when you try opening a python 3 file in the wrong directory. The truth is Jupyter and Python are two different software entirely. So, a kernel error occurs when Jupyter fails to connect with a specific version of Python.

How does VS Code connect to Jupyter?

Select the Jupyter Server: local button in the global Status bar or run the Jupyter: Specify local or remote Jupyter server for connections command from the Command Palette (Ctrl+Shift+P). When prompted to Pick how to connect to Jupyter, select Existing: Specify the URI of an existing server.

Why the code is not running in Jupyter Notebook?

Jupyter doesn't load or doesn't work in the browserTry disabling any browser extensions and/or any Jupyter extensions you have installed. Some internet security software can interfere with Jupyter. If you have security software, try turning it off temporarily, and look in the settings for a more long-term solution.

How do I keep the Jupyter Notebook running on a remote server?

This can be done by typing jupyter notebook in the terminal, which will open a browser. Then, navigate to the respective jupyter notebook file in the browser and open it. Click Cell > Run All on the toolbar. All done!


2 Answers

You need to select the python interpreter for jupyter, you can do it by following the step

  1. Open command panel

    Mac: CMD+Shift+P

    PC: CTRL+SHIFT+P

  2. Then search for select Interpreter to start jupyter server then hit enter, it will list all the interpreter, then select any interpreter and done!

it's just a one time process, after this, it will get connected automatically.

like image 151
Ankit Kumar Avatar answered Oct 08 '22 04:10

Ankit Kumar


As bizarre as it seems, I also noticed this the other day and the only thing that works so far for me is to open VS Code by launching it from the Anaconda Navigator:

enter image description here

Then I get, as expected:

enter image description here

like image 38
Alex L Avatar answered Oct 08 '22 05:10

Alex L