Search code examples
pythonvisual-studio-codejupyter-notebookvscode-code-runner

Can the Code Runner extension in VS Code use the selected kernel when running a piece of code?


I'm new to programming and I'm setting up setting up Visual Studio Code to start learning some Python. I've got the following setup:

  • MacOS 10.6.4 (I've got a system Python 2.7.16)
  • installed Anaconda ('base' environment with Python 3.9.7 and 'Python310InclCoreDS' environment with Python 3.10.4)
  • installed Visual Studio Code 1.66.2
    • ext: Python 2022.4.1
    • ext: Pylance 2022.4.3
    • ext: Jupyter Notebook Renderers v1.0.6
    • ext: Jupyter Keymap v1.0.0
    • ext: Jupyter 2022.3.1000901801
    • ext: Code Runner 0.11.7
    • ext: Black Formatter v2022.1.11231003

Now I have some trouble understanding the way the Code Runner extension picks it's Python version. See the screenshot below.

Visual Studio Code screenshot

  • Whenever I run a complete regular Python file (.py) the Python kernel picker works as expected and the selected Python version is used. (This scenario is not in the screenshot.)
  • Whenever I run a code cell in a Jupyter notebook using the "Execute Cell" button the Jupyter kernel picker works as expected and the selected Python version is used. (This scenario is shown in my screenshot in red.)
  • Whenever I select a piece of code using Code Runner in either a regular Python file (.py) or a Jupyter notebook the kernel pickers don't work as expected. I've noticed it will use my system Python 2.7.16 if terminal.integrated.inheritEnv=True. (This scenario is shown in my screenshot in blue.) It will use my Python 3.9.7 Anaconda environment when terminal.integrated.inheritEnv=False. (This scenario is not in the screenshot.)

Now I wonder: can Code Runner when running a piece of code also use the selected kernel (either when executing a piece of a .py file, or when executing a piece of a Jupyter notebook)?


Solution

  • This should be related to the default Python interpreter used by code-runner, which you can change in setting.json.

    You can refer to this page on github for more related information. enter image description here