The world’s leading publication for data science, AI, and ML professionals.

How to Fix Kernel Error in Jupyter Notebook

Make your python codes run smoothly

Photo by Matt Ridley on Unsplash
Photo by Matt Ridley on Unsplash

Data Science involves the use of technical programming tools. Most times these tools become complex to work with due to errors in analysis, system compatibility, technical difficulties, or errors from the software developers.

All developers must be familiar with Anaconda Navigator, right?

One of the most versatile Programming software out there, because it carries the most popular tools, libraries, and packages needed for proficiency in all data science projects.

Among these libraries is the Jupyter Notebook. A tool used to create, develop and share files that contain live codes, visualizations, and narrative texts.

However, there are certain errors associated with using the notebook. On launch or while working on a project, there is a high chance you will come across a Kernel Error.

Before we get into the solutions, what is a kernel error?

A kernel error occurs basically when you try opening a Python 3 file in the wrong directory. The truth is Jupyter and Python are two different software entirely. So, a kernel error occurs when Jupyter fails to connect with a specific version of Python.

When the system encounters difficulties locating the path which certain programs will run, this error is known as a kernel error in Jupyter Notebook.

Now that we are familiar with Jupyter Notebook and what the error is all about, let’s dive into the solution.

Enough theory, let’s get practical.


1. Access the back-end of the tool.

The Jupyter Notebook most of us work with runs on Anaconda. So to get to the back-end, open the Anaconda command prompt. Since you have Anaconda already installed in your system, click the search bar, type Anaconda prompt and run as administrator.

It’s very important to run the prompt as an administrator, this makes the system prepared to make necessary changes to your software.

Screenshot by author.
Screenshot by author.

2. Open Base Environment and select your directory.

The directory is where you want all your present and future projects to be stored. In less technical terms, it is choosing like a massive folder with easy access to keep your notes, text files, and libraries for recollection and transfer.

The black screen contains a preset directory which isc:/windows/system32. However, it is recommended that you switch to your preferred directory before running codes on the notebook.

I do most of my development and analysis on my e:drive, under the folder linux. This is where I will work on further libraries and other analytical projects. Input the command and change the directory of your notebook.

Screenshot by author.
Screenshot by author.

3. Create another environment for your notebook.

Most kernel errors are often brought about by the failure of the Notebook to connect with other versions of Python.

By default, there are no other virtual environments in Jupyter Notebook apart from Python 3.

Press CTRL + C and create a new virtual environment using this code.

conda create -n py

For this tutorial we will be using py you can use any environment you are comfortable with, it doesn’t necessarily have to be py just make sure to replace mine with yours while running the command.

Screenshot by author.
Screenshot by author.

4. Activate the environment you just created.

Use the code below to activate it

conda activate py

after activation, you can observe that the environment has changed from base to py this is a confirmation that the activation of the new environment worked perfectly.

Next up?

5. Install ipykernel.

Install ipykernel using the code below.

conda install ipykernel

I’m sure you’re wondering, what is ipykernel?

During its early stages, Jupyter notebooks used to be called Ipython Notebooks. So ipykernel is basically a way to connect and manage all of the different kernels.

Screenshot by author.
Screenshot by author.

6. Add kernel to the new environment.

Now we will run this next line of code to merge the kernel to our virtual environment and install it for this user.

python -m ipykernel install - user - name myenv - display-name "py"
Screenshot by author.
Screenshot by author.

That’s all about solving issues related to kernel errors. You can go ahead to restart Jupyter Notebook if you want to test out the changes immediately or you can install some of the valuable libraries you want in this new environment.

Use the codes below:

conda install azure
conda install pandas
conda install backports

Resources:

Installing the IPython kernel


Related Articles