Handling import errors in Jupyter notebooks

Creating Kernels to deflect package import errors in a conda environment

Ishika Johari
3 min readMar 21, 2021

Notebooks have forever been the favourite to use when it comes to convenience and scalability, be it for your exploratory data analysis on a specific dataset, or running usual scripts on a kernel instead (such as scraping data to say). Nevertheless, they come with their own challenges, especially when working locally through a new conda environment.

If you’ve ever tried to use Jupyter notebooks locally through Anaconda, but experienced import errors for the most common of packages, (Pandas and Seaborn, I’m onto you 😾) this article is for you :)

Following are the basic steps to create a new conda environment from scratch. Feel free to skip through these towards the next section!

Steps:

  1. Move to your preferred directory using:
cd <dir_path> 

(This step can be done after step 3 as well)

2. To create a new conda environment:

conda create -n <env_name> python=3.6

3. To activate this environment on every use:

conda activate <env_name> 

or

source activate <env_name>

After activating an environment, your prompt must look similar to the following:

(<env_name>) $ 

To open the Jupyter notebook home:

(<env_name>) $ jupyter notebook
When opening Jupyter locally

Adding a Kernel to prevent import errors

(Don’t do this)

When opening a new notebook in your directory here, if you open it through ‘Python’:

You’ll face countless import errors (not attaching those painstaking screenshots 😣)

(Do this instead)

Go to your Terminal/Anaconda command prompt from the same directory inside the same environment and add a kernel using:

(<env_name>) $ python -m ipykernel install --user --name=<env_name> 

or

(<env_name>) $ ipython kernel install --name "<env_name>" --user

Now when you open a new notebook, open it through this newly created kernel:

Now try to import a package! (Check the installation process below)

All of the above is ensured provided you have the respective packages already installed in that specific environment.

You can do this either through the Terminal/Anaconda command prompt:

(<env_name>) $ pip install <package_name>

or through the Anaconda navigator interface as follows:

Go to your environment under the ‘Environments’ tab from the left.
Search for your package from the ‘Not installed ‘category

--

--

Ishika Johari

Data Science Analyst @ Dell Technologies | Steadfast Learner | Just a tyro at Data Science