Copilot Explains – Error troubleshooting in Jupyter Notebooks

This post has been republished via RSS; it originally appeared at: Microsoft Tech Community - Latest Blogs - .

Data scientists and AI engineers love to work with Jupyter Notebooks because they make so much easier to look at the result of each and every data exploration step or data modeling experiment and make decisions accordingly.

However, Jupyter notebooks are not immune to errors, and sometimes understanding error messages - in particular if you aren’t a native English speaker or you are a beginner - and troubleshooting code might be painful and time-consuming.

In this video below I share some tips and tricks on how to leverage VSCode and GitHub Copilot to increase your productivity, making it easier to analyze and fix the most common Python errors in Jupyter notebooks.

 

 

The demo

The code shown in this video is hosted in a GitHub repo.

The original Jupyter Notebook - version without errors - along with the related documentation used in this video is taken from Lesson 4 - Logistic Regression of the Machine Learning for Beginners open-source curriculum.

 

To replicate the demo on your local machine, you need the following pre-requisites:

  1. Install VS Code on your machine
  2. Sign up for a GitHub Copilot free trial
  3. Install GitHub Copilot and GitHub Copilot Chat extensions on VS Code
  4. Install the Python extension and the Jupyter extension to work with Python Jupyter notebooks in VS Code
  5. Create a virtual environment with numpy and pandas packages installed

In the demo repo, you also have the option of running the Jupyter notebook on GitHub Codespaces.

 

Errors

This video covers 3 types of common errors when working with Python in Jupyter Notebooks.

 

1. Indentation Error

Whereas in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code, so you have to use the same number of spaces in the same block of code, otherwise Python will give you an error, like in the example below.

IndentationError.png

 

2. Module Not Found Error

When you try to import in your code a library that is not installed in your development environment, you'll encounter a ModuleNotFound error. In our example below, we are trying to use the seaborn library, which is not installed in our venv.

ModuleNotFound.png

 

3. Name Error

NameError usually occurs when you try to use a variable that has not been defined before usage or it's out of scope. Sometimes it can be due simply to a typo, making the variable name declared and used not match, as in the code cell below.

NameError.png

 

GitHub Copilot Features

This demo leverages some of the GitHub Copilot features available in VS Code to troubleshoot and fix the errors above:

 

  1. Inline suggestions: GitHub Copilot completes your code and markdown cell content as you type
  2. Chat: GitHub Copilot offers a chat experience (both inline and in the sidebar) to get assistance in VS Code, avoiding context-switching
  3. Several slash commands, such as:

   - /createNotebook -> to create a Jupyter Notebook with some specifications

     Note that this command has been replaced by `@workspace /newNotebook` with the latest release introducing agents.

   - /fix -> to get a suggestion to fix an error

   - /vscode -> to ask questions regarding working with VS Code

     Note that this command has been replaced by `@vscode` with the latest release introducing agents.

   - /explain -> to ask for clarifications about code or error messages

   Note that this command has been replaced by `@workspace /explain` with the latest release introducing agents.

   - /clear -> to clear the chat window

   - /doc -> to get an AI auto-generated documentation of your code

 

Useful resources

To learn more about how to leverage GitHub Copilot in VS Code for different scenarios, check out the following resources:

- Supercharging Your Data Science Projects with GitHub Tools webinar on ODSC

- Copilot Explains video series

- GitHub Universe Cloud Skills Challenge on Microsoft Learn

 

Copilot is updated with great changes/features often, so please refer to the latest release notes.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.