Search code examples
pythonmacosvisual-studio-codejupyter-notebookpyenv

Visual Studio Code - How to set (missing) Python kernel - Jupyter Notebook


Background

  • I am on macOS 12.6.1
  • I installed pyenv via homebrew
  • I installed 3.10.7 via pyenv pyenv install 3.10.7
  • I set 3.10.7 to global with pyenv global 3.10.7
  • I have installed ipykernel with `pip install ipykernel'
  • My .zshrc file contains the following:
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Under 'Python: Select interpreter' I have four options:

  • Enter interpreter path...
  • Use Python from 'python.defaultInterpreterPath' setting ~/.pyenv/versions/3.10.7/bin/python
  • Python 3.10.7 64-bit('3.10.7':penv) ~/.pyenv/versions/3.10.7/bin/python Recommended
  • Python 3.9.6 64-bit /usr/bin/python3 Global

If I type `python --version' in the terminal, I get Python 3.10.7


If I click on the kernel selection button (top right), I have the following option:

  • Python 3.9.6 64-bit /usr/bin/python3 Currently selected Suggested
  • Connect to a Jupyter Server

Could someone clarify/help me understand the interpreter vs kernel settings in Visual Studio Code?

How can I add the option of using Python 3.10.7 to the kernel?


Solution

  • I've managed to get Python 3.10.7 to become available in the kernel option. What I did was:

    1. File -> Open folder -> Desktop
    2. Made a new python file, test.py, which contained some python code:
    a = 4
    b = 2
    c = a + b
    print(c)
    
    1. Ran the python file, made sure that output (c) was printed.
    2. Then cmd + shift + p + navigate to 'Jupyter filter kernels'
    3. Selected 3.10.7 and unselected Python 3.9.6
    4. Made a new jupyter notebook file, and the kernel option was already preselected as 3.10.7.

    I don't know which part of my actions actually were required for it to get working, I'm sure that someone of them were not relevant, but it's working now.