Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter notebook with Python 2 and Python3 Kernel

I want to run Python2 as well as Python3 kernel from Jupiter notebook. I am using Anaconda for Python and Jupyter distribution.

Lokeshs-MacBook-Air-2:~ lokeshagrawal$ conda --version
conda 4.5.12

Lokeshs-MacBook-Air-2:~ lokeshagrawal$ which python
/anaconda3/bin/python

Lokeshs-MacBook-Air-2:~ lokeshagrawal$ which jupyter notebook
/anaconda3/bin/jupyter

[![Lokeshs-MacBook-Air-2:~ lokeshagrawal$ python --version
Python 3.7.2

As you can see in the image below, I only have an option to start Python3 kernel from Jupyter. How can I have Python2 and Python3 both?

enter image description here

like image 875
Lokesh Agrawal Avatar asked Mar 17 '19 06:03

Lokesh Agrawal


2 Answers

  1. Make sure you have pip version greater than 9.0
$ python2 -m pip --version
  1. Then do this
$ python2 -m pip install ipykernel OR python2 -m pip install ipykernel --user
$ python2 -m ipykernel install --user
  1. Start or restart Jupyter and you should be done.

This solution is from the ipython docs by the way.

like image 131
Dylan Avatar answered Oct 10 '22 02:10

Dylan


You can do:

conda create —name py2 python=2.7 anaconda 
conda activate py2
(py2) conda install ipykernel -y
(py2) python -m ipykernel install --user --name py2 --display-name "Python 2.7"

This creates an environment called py2 with Python 2.7 and adds it to your kernel with name Python 2.7

If we want to have other versions e.g. Python 3.7 also, we can do the same steps:

conda update conda
conda create —name py3 python=3.7 anaconda 
conda activate py3
(py3) conda install ipykernel -y
(py3) python -m ipykernel install --user --name py3 --display-name "Python 3.7"

Note: you do not have to add 'anaconda' packages. Hope this helps you understand how to add environments to your jupyter kernel.

See: Anaconda Documentation

like image 29
Prayson W. Daniel Avatar answered Oct 10 '22 01:10

Prayson W. Daniel