10

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

2 Answers 2

5
  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.

Sign up to request clarification or add additional context in comments.

7 Comments

No it didnt give any error, but when I restarted notebook after performing steps above list still shows just a Python3
I am not sure if this has anything to do with Conda.
try shutting jupyter down completely from the cli and then try?
I doubt it has anything to do with conda, because i'm using it too
Thanks Dylan for helping out.
|
2

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

1 Comment

Always a pleasure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.