question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jupyter running wrong python kernel

See original GitHub issue

Hi everyone, I want to report that Jupyter is loading the wrong Kernel

My machine is a Debian Testing, Linux 4.9.0-3-amd64 #1 SMP Debian 4.9.30-1 (2017-06-04) x86_64 GNU/Linux

Python2.7 is installed by default, Python3 was installed from repositories, but I’m not sure if it comes by default with the system too. Jupyter and Ipython where installed via Pip (Pip was installed from the repositories)

Module Versions Installed via pip with dependencies pip install -U jupyter ipython pip3 install -U jupyter ipython

So far, the versions installed are,

  • Jupyter version 4.3.0 (PIP2 and PIP3)
  • Python 3.5.3 from repos
  • Python 2.7.13 from repos
  • Ipython 5.4.1 (PIP2)
  • Ipython3 6.1.0 (PIP3)

Proceeded to install the corresponding kernels as root with the commands, ipython kernel install ipython3 kernel install

Which seemed to work since Jupyter recognizes both kernels

image

And at the location /usr/local/share/jupyter/kernels there’s this tree architecture

image

With the relevant contents,

$ cat python2/kernel.json 
{
 "display_name": "Python 2", 
 "language": "python", 
 "argv": [
  "/usr/bin/python", 
  "-m", 
  "ipykernel_launcher", 
  "-f", 
  "{connection_file}"
 ]
}
$ cat python3/kernel.json 
{
 "display_name": "Python 3",
 "argv": [
  "/usr/bin/python3",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "language": "python"
}

I try to program with Python3 standard since 2.7 is old, so Installed a module I wanted to use by means of pip3 install module_name. The module worked with python3 console and Ipython3 console, but when I tried to implement the code inside a Jupyter notebook (with kernel Python3) and I got an error telling that the module wasn’t available. After being unable to solve the problem I installed the module with pip2 and suddenly it was available at the Jupyter notebook (with kernel Python3) so I performed a simple check of the version of the running kernel under Jupyter,

image

As you can see, it says that the kernel is Python3, but the call to sys.version (from the module sys) still shows that the running kernel is Python2.7. I don’t know what’s going on, even resetted Jupyther’s configuration with jupyter notebook --generate-config and the issue remains unsolved, that’s why I decided to post here.

On the other hand, calling the deprecated form ipython3 notebook produces the desired result,

image

Wath am I missing?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:34
  • Comments:47 (11 by maintainers)

github_iconTop GitHub Comments

74reactions
Daniel-Mcommented, Jun 9, 2017

@takluyver off course, as you thought there was an override of the kernels. The output of jupyter kernelspec list was,

Available kernels:
  python3    /home/damejia/.local/share/jupyter/kernels/python3
  python2    /usr/local/share/jupyter/kernels/python2

The kernel for python3 located in /home/damejia/.local/share/jupyter/kernels/python3 was incorrectly configured. The kernel.json read,

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

So it called the wrong python version (python points to Python 2.7 in my machine). I just removed the /home/damejia/.local/share/jupyter/kernels/ path and now jupyter kernelspec list shows,

Available kernels:
  python2    /usr/local/share/jupyter/kernels/python2
  python3    /usr/local/share/jupyter/kernels/python3

And under a Python3 kernel in Jupyter the output of,

import sys
sys.version

baheves as expected,

'3.5.3 (default, Jan 19 2017, 14:11:04) \n[GCC 6.3.0 20170118]'

I’m not sure why this happened, I’ve been using Jupyther for about a year without problems so far. I hope this thread can help anyone. I guess It’s closed 👍

62reactions
rahulremanancommented, Dec 2, 2017

I also encountered a similar error. Jupyter notebook installation in my Ubuntu 17.04 cloud machine had issues selecting the right kernel. This is what I did to solve this issue.

Step 1: Install all the necessary dependencies: For Python2: $sudo apt-get install python-setuptools python-dev build-essential $sudo easy_install pip

$python2 -m pip --version

$python2 -m pip install ipykernel $sudo python2 -m ipykernel install --user $pip install -U jupyter ipython

For Python3: $sudo apt-get install python3-setuptools python3-dev build-essential $sudo apt-get install python3-pip

$python3 -m pip --version

$sudo python3 -m pip install ipykernel $sudo python3 -m ipykernel install --user $sudo pip3 install -U jupyter ipython

Step 2: Find the python executable locations for each version of Python and available kernels for Jupyter:

For Python2: $python

import sys print sys.executable The output for me was: /usr/bin/python

For Python3: $python3

import sys print (sys.executable) The output for me was: /usr/bin/python3

To get the list of available kernels:

$jupyter kernelspec list

The output for me was: Available kernels: python2 /home/info/.local/share/jupyter/kernels/python2 python3 /home/info/.local/share/jupyter/kernels/python3

Step 3: Update the kernel.json file with the correct location pointers to python executable

For Python2: $sudo nano /usr/local/share/jupyter/kernels/python2/kernel.json

Eg:

{ “display_name”: “Python 2”, “language”: “python”, “argv”: [ “/usr/bin/python”,
“-m”, “ipykernel_launcher”, “-f”, “{connection_file}” ] }

For Python3: $sudo nano /usr/local/share/jupyter/kernels/python3/kernel.json

{ “display_name”: “Python 3”, “language”: “python”, “argv”: [ “/usr/bin/python3”,
“-m”, “ipykernel_launcher”, “-f”, “{connection_file}” ] }

Step 4: Restart Jupyter and toggle kernels to see if python versions are selected correctly in the back-end.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jupyter using the wrong version of python - Stack Overflow
For example my python kernel is located at: /usr/local/share/jupyter/kernels/python/kernel.json. and contains: { "language": "python" ...
Read more >
Jupyter notebook running the wrong python version
When multiple versions of python are installed in a system along with anaconda3, jupyter kernels might run the wrong python version.
Read more >
What to do when things go wrong - The Jupyter Notebook
What to do when things go wrong . First, have a look at the common problems listed below. If you can figure it...
Read more >
How to Fix Kernel Error in Jupyter Notebook
A kernel error occurs basically when you try opening a python 3 file in the wrong directory. The truth is Jupyter and Python...
Read more >
Jupyter Kernel not running same Python version as the Virtual ...
Install Jupyter Notebook through pip (instead of pacman) within the virtual environment set on Python 3.7 (This is not at all recommended for ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found