Set up Jupyter Notebook in VS Code for Data Science (2024)

Ajeet Yadav

Posted on • Updated on

Set up Jupyter Notebook in VS Code for Data Science (3) Set up Jupyter Notebook in VS Code for Data Science (4) Set up Jupyter Notebook in VS Code for Data Science (5) Set up Jupyter Notebook in VS Code for Data Science (6) Set up Jupyter Notebook in VS Code for Data Science (7)

#beginners #vscode #jupyter #python

Blog | Twitter

Originally published on my blogging site ajeet.dev

I will write several blog posts on Python. All of them will be focused on the beginners to get them started with Python for Data Science. Follow the updates here : Learn Python Step by Step - A Blog Series by Me 🚀. Don't forget to subscribe to my weekly Newsletter 📧

I am a newbie in tech. This blog post is based on my personal experience. Feedback Welcome. Would really appreciate your time 🙏

Project Jupyter is a non-profit, 100% open-source project. It develops software and web applications to support interactive data science and scientific computing. JupyterLab, Jupyter Notebook, and Jupyter Hub are the three key open source software developed by the team.

Set up Jupyter Notebook in VS Code for Data Science (8)
Jupyter Logo is taken from Jupyter.org

The Jupyter Notebook isa web app that lets you easily create and share documents that contain your live source code, markdown text, equations and visualizations - all in one canvas called a Notebook.It supports dozens of programming languages such asPython, R, Scala, Spark, and Julia.

Data scientists use Jupyter Notebooks for several tasks - data analysis, cleaning, transformation, modelling, visualizations, machine learning, and so on. You can easily explore data using the popular Python libraries such as pandas, scikit-learn, ggplot2, TensorFlow.

You can easily set up and use Jupyter Notebook with Visual Studio Code, run all the live codes and see data visualizations without leaving the VS Code UI.

This blog post is a step-by-step guide toset up and use Jupyter Notebook in VS Code Editor for data science or machine learning on Windows. The post is written exclusively for the beginners in tech.

Table of Contents

  • 🛠️Set up our environment - Create a Jupyter Notebook
  • ⚙️ Work with code cells in the Notebook Editor
  • 🔥Key Features of Jupyter Notebook Editor
  • 🤝Python Interactive - A Mix of Jupyter Notebook & Python Script
  • 🔥Key Features ofIPython Interactive
  • 💡 What's Next
  • 🧰Additional resources for continued learning

Set up Jupyter Notebook in VS Code for Data Science (9)

🛠️Set up our environment - Create a Jupyter Notebook

To set up your environment, you can either use a combination of WSLand VS Code, orWindows 10 with VS Code. I am using the former -Windows Subsystem for Linux (WSL) on Windowsplus VS Code. Read this guide why I use WSL.

We will set up our Jupyter environment by creating a new Jupyter Notebook. If you have followed my WSL guide, you would have seen Ubuntu terminal by now. Let us create a Jupyter notebook. Open VS Code integrated terminal. Here is the quickest way to open VS Code integrated terminal inside Windows Subsystem for Linux (WSL). This will automatically open the Remote WSL. The view should look like this:

Set up Jupyter Notebook in VS Code for Data Science (10)

Now, pressCNTRL+SHIFT+Pbutton simultaneously using your keyboard. This will bring up a dropdown view in the VS Code Editor view. Enter Python: Create New Blank Jupyter Notebook andselectit from the dropdown. Clicking on it should load Python extension if not loaded before.

Set up Jupyter Notebook in VS Code for Data Science (11)

Once connected, the first view should look like this:

Set up Jupyter Notebook in VS Code for Data Science (12)

The top right of the VS Code UI says "Jupyter server: Not started". This means Jupyter library is currently not installed inside WSL. You will also get two pops, one of which says, "Data science libraries notebook and jupyter not installed". Click on "Install". It will install Jupyter. Let it first install automatically.

Next, click on "Yes" prompt where it says "Data Science library ipykernel is not installed. Install?".

Set up Jupyter Notebook in VS Code for Data Science (13)

One thing is to note that, you may see a pop up saying "Error: 'Kernelspec' module not installed in the selected interpreter ({0}). Please re-install or update 'jupyter' ".

Set up Jupyter Notebook in VS Code for Data Science (14)

Many people have reported this. Here is a fix taken fromofficial VS Code Python extension. In the integrated VS Code terminal, run both the commands one by one:

python3 -m pip install --upgrade pip

python3 -m pip install jupyter

Restart VS Code editor, and you should not see the pop up now. Let me know in the comments if the issue still persists. We will fix it together.

Congrats! You have set up the Jupyter library in VS Code inside the Windows Subsystem for Linux. I have written something in my Note. The final view should look like this.

Set up Jupyter Notebook in VS Code for Data Science (15)

⚙️ Work with code cells in the Notebook Editor

In the view, you should see M with a down arrow. Just belowthis, you will find cells to type in your code. Copy and paste these lines:

hello = "I am learning Python"

print(hello)

Set up Jupyter Notebook in VS Code for Data Science (16)

To run this code,click onthe green run icon next to the cell. This will run the code cell.

Set up Jupyter Notebook in VS Code for Data Science (17)

The output of the code cell will appear just below the code cell.

Set up Jupyter Notebook in VS Code for Data Science (18)

Now, we will save our Jupyter notebook in our desired directory. I will save it in "hello_python" folder. PressCNTRL+Sbutton simultaneously using your keyboard.

Set up Jupyter Notebook in VS Code for Data Science (19)

Set up Jupyter Notebook in VS Code for Data Science (20)

Note the format of the file once you save it, it will be .ipynb format.

Set up Jupyter Notebook in VS Code for Data Science (21)

🔥Key Features of Jupyter Notebook Editor

The VS Code Jupyter integration is loaded with a lot of features.

🤝Python Interactive - A Mix of Jupyter Notebook & Python Script

One of the finest features of the Python VS Code extension is the hybrid
approach to use Jupyter notebook and a Python script. Confused? This is
the Python Interactive window. Once we set this up, the final view will
look like this:

Set up Jupyter Notebook in VS Code for Data Science (22)

Save the .ipynb file as python script. Click on the "convert and save to a
Python script" option located at the top of the Editor view.

Set up Jupyter Notebook in VS Code for Data Science (23)

Once you click on the "convert and save to a Python script", VS code editor
will open up a new file. Here is the view of the Untitled-1 file. The format of the file is .py

Set up Jupyter Notebook in VS Code for Data Science (24)

Save this file and rename it whatever you like it to. Make sure the file extension is .py. My file name is PythonScript. Click on OK.

Set up Jupyter Notebook in VS Code for Data Science (25)

Once you rename the file, the view will look like this:

Set up Jupyter Notebook in VS Code for Data Science (26)

You will get three options in this view - Run Cell, Run Above, and Debug Cell. Click on the options to see the results.

Now, in the Python extension's settings, we need to ticktheData Science: Debug Just My Codeoption. To do this, press CTRL+,button simultaneously using your keyboard. This will open up the Settingsbox. In the search box, paste this:Data Science: Debug Just My Code. Select this Option. Since I am using Windows Subsystem for Linux environment, I have first clicked on Remote [WSL:Ubuntu-18.04] and then checked the Debug Just My Code.

Set up Jupyter Notebook in VS Code for Data Science (27)

Now, close the Settings option. Let us get the Ipython Interactive view. In
the file PythonScript.py file, click on any cell & pressSHIFT+Enterkeyboard button.Thiscommand opens up thePython Interactive window with the output of the code cell you just ran. It will have three sections: Python Script file code view, IPython Interactive, and a Console to run command.

Set up Jupyter Notebook in VS Code for Data Science (28)

Clickon any code cell in the PythonScript window & pressSHIFT+Enterkeyboard buttonto get the output in the IPython Interactive window.

One of the key features of the IPython Interactive is the Console window in which you can run any code and get the output of that single code. Just enter your code andpressSHIFT+Enterkeyboard button.

Set up Jupyter Notebook in VS Code for Data Science (29)

Set up Jupyter Notebook in VS Code for Data Science (30)

🔥Key Features ofIPython Interactive

The Ipython Interactive window has almost all the features of the Jupyter Notebook.

💡 What's Next

I am going to write about data structure and algorithms in Python. Meanwhile, if you are a newbie in Python, follow this track.

Newbie in Python? Follow this track

I will add many more posts under this track. Stay Tuned. Subscribe to my Blog.

🧰 Additional resources for continued learning

Here are the recommendations to learn more about VS Code Python extension's usage.

Credits 🙏 :
Official Microsoft Docs on Jupyter Notebook

Emojis taken fromEmojipedia

Cover image built with BlogCover

Set up Jupyter Notebook in VS Code for Data Science (31)

Top comments (8)

Subscribe

Alara Oluwatoyin Joel

Alara Oluwatoyin Joel

Javascript Dev, who wants to build amazing things with the MERN stack. I also have an interest in Data science and Machine-learning.Getting better every day. Filled with wisdom from above!!

  • Location

    Accra, Ghana

  • Education

    B.Sc Clinical Physiology

  • Joined

Jul 23 '20

  • Copy link

great write I used jupyter online to start learning DS, now i want an offline environment, please honestly compare Jupiter and anaconda, which will be perfect for me?
thanks

Ajeet Yadav

Ajeet Yadav

  • Location

    India

  • Joined

Jul 23 '20

  • Copy link

Thanks Alara :)

Anaconda is all about DS/ML. It has its own Python package manager called conda, and it supports pip as well. Managing virtualenv is easy, no need to specify a directory where you want the environment to be set up.

Without a doubt, go for it :)

Alara Oluwatoyin Joel

Alara Oluwatoyin Joel

Javascript Dev, who wants to build amazing things with the MERN stack. I also have an interest in Data science and Machine-learning.Getting better every day. Filled with wisdom from above!!

  • Location

    Accra, Ghana

  • Education

    B.Sc Clinical Physiology

  • Joined

Jul 27 '20

  • Copy link

Heard a lot about ipython interpreter, but anaconda sounds so so robust, it's like a world of it's own

Ajeet Yadav

Ajeet Yadav

  • Location

    India

  • Joined

Jul 28 '20

  • Copy link

Try JupyterLab once you insall Anaconda. You will fall in love with it.
Here is how it looks.

Alara Oluwatoyin Joel

Alara Oluwatoyin Joel

Javascript Dev, who wants to build amazing things with the MERN stack. I also have an interest in Data science and Machine-learning.Getting better every day. Filled with wisdom from above!!

  • Location

    Accra, Ghana

  • Education

    B.Sc Clinical Physiology

  • Joined

Sep 1 '20

  • Copy link

Jupyter lab seems more intuitive than Jupyter notebooks although i got both of them to work offline

What I am trying to wrap my head around now is the whole environments thing in anaconda.

It seems to me that packages are different from the tools(Matplotlib, NumPy ... ) (Jupyter, kite, Ipython ...)
So the tools are always available in every environment, but the packages have to be installed I think, as they are only available in the base environment, which isn't encouraged to be worked on.

My question is, when we install a package (say numpy), that is already in the base environment, is it installed fresh from the internet, or is it just copied over from the base conda environment. ?

Ajeet Yadav

Ajeet Yadav

  • Location

    India

  • Joined

Sep 6 '20

  • Copy link

If you are using Anaconda distribution, you don’t need to separately installing NumPy or any of the major packages , like pandas, Scikit-Learn... To use NumPy, you just run the command import numpy as np.

But if you have not installed Anaconda, but you have Python installed, in that case you have to install it using conda install numpy or pip install numpy. Installing will require an internet connection this time. Then you will import it - import numpy as np

Alara Oluwatoyin Joel

Alara Oluwatoyin Joel

Javascript Dev, who wants to build amazing things with the MERN stack. I also have an interest in Data science and Machine-learning.Getting better every day. Filled with wisdom from above!!

  • Location

    Accra, Ghana

  • Education

    B.Sc Clinical Physiology

  • Joined

Jul 27 '20

  • Copy link

Sure boss!! that settles it!!!

_Lil_

_Lil_

  • Joined

Mar 9 '21 • Edited on Mar 9 • Edited

  • Copy link

Hi, I have exactly the problem you described: "Error: 'Kernelspec' module not installed in the selected interpreter ({0}). Please re-install or update 'jupyter' ".
I tried your fix, and it says Requirements already satisfied, but when I restart VScode I still get the exact same problem when I want to Select Interpreter for Jupyter.
Any other idea what to try?

For further actions, you may consider blocking this person and/or reporting abuse

Set up Jupyter Notebook in VS Code for Data Science (2024)
Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6330

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.