Integrating Python and Jupyter Notebook with Visual Studio Code

Question:

Summary:

I am trying to set up the Jupyter Notebook extension for VSCode but I can’t get my Python code snippets to run. There is no "Run" button to select. I can’t select a Python/ Jupyter kernel to work with.

The problem in greater detail:

If you check out this YouTube clip at exactly 1 minute and 46 seconds (here – – you can pause it to see the still) this is what I am trying to accomplish.
But here is what my VSCode actually looks like:

My VSC showing missing features

As you can see in my screenshot, the “Play” icon to the left of my code snippet is missing. Along the top border, the other “Play All” button is missing.

In Microsoft’s official VSC doc for setting up Jupyter Notebooks, right near the top under this specific heading: “Create or open a Jupyter Notebook” their interface has way more options at the top of the cells when compared to mine. They have buttons named: “Variables”, “Restart”, “Interrupt”. That is expected for Jupyter Notebook funcionality. Mine is missing all of that. Clearly there is something wrong with my setup.

Other SO users report having ms-python extension installed (which includes Jupyter functionality) in addition to stand-alone Jupyter Notebook extensions installed. Apparently in situations when you have multiple Python extensions installed, they can interfere with each other. So after much wrangling, I resolved to uninstalling all my Jupyter extensions and keeping only the main ms-python extension.

The issue persists.

Here is a complete list of all my VSC installed extensions:

$ code --list-extensions --show-versions
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

There is a bit of noise in that list. I’m not sure what all of them refer to. But for certain there is only one Python app (ms-python) and nothing related to Jupyter.

Some SO members elsewhere have suggested selecting / setting the base Python version/environment. When I click the “Select Kernel” button at the top right (as illustrated in the screenshot below), it reveals an empty menu (see at the top left):

No Kernel options

Here is “Python v3.8.5 (conda)” showing as selected at the bottom and slightly to the left:

Python v3.8.5 (conda) showing

Another exhaustive question and answer thread elsewhere on Stack Overflow involving an issue similar to mine includes many, many creative potential solutions: jupyter server : not started, no kernel in vs code
In that Stack Overflow question, one prominent member answers:

How I solved it:

  1. Press Command+Shift+P to open a new command pallete
  2. Type >Python: Select Intepreter to start jupyter notebook server
  3. Open the notebook again

Someone else suggests:

  1. Press Command+Shift+P on mac, Ctrl+Shift+p on windows
  2. Type Jupyter: Select Interpreter to start Jupyterserver
  3. It would show you a dropdown of python versions installed.
  4. I chose python 3.7.5 and it worked for me. you can choose the python version installed on your machine.

Another member says:

I have seen all possible solutions but not work, finally I just
upgrade jupyter, notebook, and jupyterlab, like pip install -U jupyterlab, and I can choose the kernel in VScode!

None of the above resolves my issue.

Here is a quirky new twist: After installing jupyterlab with pip, after restarting VSC, I encountered this weird error:

enter image description here

It’s a condition box. I clicked: “Yes”.

Jupyter is still not integrated properly.

UPDATED: Based on JialeDu’s answer, I followed his instructions. I created a video shared below. It doesn’t quite capture every step, but I did follow all the steps. What you can see in the gif video is that my current working directory is a folder called Test, a new terminal has been opened, and there is an active virtual environment with $ source .venv/bin/activate. Next, as you can see in the .gif, I am able to select the latest Python interpreter v3.10.5 showing as “Recommended”. So that is progress. I couldn’t do that before. Going the virtual environment route was a great idea. However, I am still not able to get the Jupyter Notebook interface as the end product shared by JialeDu. When I invoke Ctrl + Shift + P again and type > create jup a new Jupyter Notebook file is created but the kernel I selected previously does not show and the expected Jupyter interface buttons and options are still not showing. Here is the video demo in .gif formatting:

Imgur

Please note: Here you can see a list of 4 available Jupyter extensions:
enter image description here

I avoided installing any of these separate Jupyter extensions in the store because according to the instructor (and as confirmed on the official Microsoft Python extension ms-python page which I already have), it includes Jupyter built in. From the ms-python extension entry, it explains: "This Python extension will automatically install the Pylance and Jupyter extensions to give you the best experience when working with Python files and Jupyter notebooks."

Even though other Stack Overflow users indicated that mixing the above extensions such as ms-python with ms-toolsai was a bad idea, I experimented with having them both installed anyway. Didn’t fix the issue. I tried uninstalling ms-python and keeping ms-toolsai. Still no dice.

By the way, I’ve also got the latest version (as of this writing) of VSCode v1.70.1 installed:
enter image description here

Asked By: Angeles89

||

Answers:

VS Code has prompted you to install the suggested extension. Python and Jupyter.

enter image description here

Also it helps to make sure your vscode is up to date.

The play button of the interface is provided by Jupyter, but if you want to run the code (select the kernel), you must also install the Python extension.

It’s a condition box. I clicked: “Yes”.

You don’t have to click Yes because there is a time delay. By the time you click Yes, the environment may not be ready.

If you are using a virtual environment, you can select the virtual environment’s interpreter and then create a new terminal to activate the environment.

Complete creation steps:

Prerequisites:

  • The machine has python installed,
  • and vscode has Python and Jupyter extensions installed.

steps

  1. Create a new working folder Test

  2. Open the folder Test with vscode

    enter image description here

  3. new terminal

    enter image description here

  4. Type the command python -m venv .venv to create a virtual environment

    enter image description here

  5. After the creation is complete, use the command .venv/scripts/activate to activate the virtual environment

    enter image description here

    Or Ctrl+Shift+P –> Python:Select Interpreter, select the interpreter of the virtual environment and then create a new terminal to activate the environment

    enter image description here

  6. Create new jupyter file using command palette command Create:New Jupyter Notebook

    enter image description here

    Or right click –> New File –> name ends with .ipynb

    enter image description here

  7. The virtual environment you just selected will be enabled as the kernel by default.

    enter image description here

Answered By: JialeDu

I discovered a very simple answer: I installed the VSC package in the official Snap repo.

VSC and Python-Jupyter look to be all playing nice together now:

enter image description here

Answered By: Angeles89