about Grigoris Maravelias
Working with both Python 2 and 3 in jupyter

Working with both Python 2 and 3 in jupyter

I have followed the pip install approach to add everything that I wanted for Python. Even though I did the same for jupyter when I was starting it (“jupyter notebook”) it would only start the Python 2 kernel, although Python 3 “was there” but not active actually.

After some digging I found that a similar issue existed already (github/jupyter/issue#270 [1]). By doing


$jupyter kernelspec list
Available kernels:
python2 /home/grigoris/.local/share/jupyter/kernels/python2
python3 /home/grigoris/.local/share/jupyter/kernels/python3

we get the locations of the kernels that are loaded. Under each of the above directories there is a kernel.json file. In more detail:

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

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

corresponding to Python 2 and Python 3, respectively. The problem lies in the version of python used, as what we see after the ‘”argv”: [‘ line is “python” in both cases. This is ok for Python 2 since this is the default way to invoke it. But not for Python 3, which needs “python3” (by default). So, the first option may be left as it is, but the second needs to change to “python3” to properly point to the correct version. However, this was not working and I found no reason why (especially since “python” and “python3” invoke the proper versions). In any case a robust solution is to provide the full paths, i.e. “/usr/bin/python” and “/usr/bin/python3”.

As I wanted to become even more specific with the versions used I made the following changes:
{
"display_name": "Python 2.7",
"language": "python",
"argv": [
"/usr/bin/python2.7",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
]
}

{
"display_name": "Python 3.7",
"language": "python",
"argv": [
"/usr/bin/python3.7",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
]
}

so now I can use both versions at the same time in jupyter. This is a manual fix to what should have been an automatic process…

[1] For more details see also this link:
https://github.com/jupyter/jupyter/issues/270

Leave a Reply

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