267

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.

I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and then IPython could be started. This is not what I was looking for so I uninstalled Anaconda and now have set up my own installation using MacPorts and PiP. It seems that I still have to use

port select --set python <python version> 

to toggle between python 2.x and 3.x. which is no better than the anaconda solution.

Is there a way to select what version of python you want to use after you start an IPython notebook, preferably with my current MacPorts build?

2

11 Answers 11

353

The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

Anaconda >= 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

conda create -n py27 python=2.7 ipykernel
conda create -n py36 python=3.6 ipykernel

After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new environments. The Managing environments section in conda's docs provides further information.

Manually registering kernels

Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

configure the python2.7 environment:

conda create -n py27 python=2.7
conda activate py27
conda install notebook ipykernel
ipython kernel install --user

configure the python3.6 environment:

conda create -n py36 python=3.6
conda activate py36
conda install notebook ipykernel
ipython kernel install --user

After that you should be able to choose between python2
and python3 when creating a new notebook in the interface.

Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.

37
  • Your solution looks a lot like the solution I ended up using and in principle I would expect it to work. Since I already uninstalled anaconda and got this working I won't be able to verify.
    – deltap
    Commented May 27, 2015 at 21:33
  • 9
    If you want to set up the kernelspecs without requiring root, you can do ipython kernelspec install-self --user to install them for the current user.
    – Thomas K
    Commented May 28, 2015 at 3:10
  • 1
    I have figured it out! Your answer helped out! dropbox.com/s/6ayqf55ctkv2xgk/… Commented Aug 25, 2015 at 10:42
  • 7
    The key item is that you must INSTALL nb_conda_kernels it did not come with my initial Anaconda! Thanks!
    – dartdog
    Commented Aug 2, 2016 at 18:16
  • 1
    @cel, indeed the environment I start jupyter from does not have the package nb_conda_kernels installed by default. I did not take the time to read all the comments: maybe you could edit your answer and include dartdog's remak. Commented Sep 27, 2017 at 13:42
300

If you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this:

python2 -m pip install ipykernel

python2 -m ipykernel install --user

http://ipython.readthedocs.io/en/stable/install/kernel_install.html

11
  • 56
    Switch the '2' for '3' if you have python2 already setup and need python3. I don't understand why this isn't the top answer, it definitely wins by Occam's razor and it worked for me. Commented Jul 30, 2016 at 9:07
  • 6
    Works perfectly. Should be the first answer indeed.
    – JSmyth
    Commented Dec 3, 2016 at 4:36
  • 2
    python2 command is included in python itself? Please give some explanation of this answer elaborately. :) Commented Jul 13, 2017 at 16:44
  • 3
    this works, but it is not linked to my system python 2 with additional packages. Is there a way to link to an existing python binary/executable? Commented Jul 21, 2017 at 1:16
  • 1
    Works perfectly in win10, just replace python2 with your\path\to\python(2).exe.
    – Lucien
    Commented Jun 15, 2018 at 14:40
45

These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.

I assume that you already have jupyter notebook installed.


First make sure that you have a python2 and a python3 interpreter with pip available.

On ubuntu you would install these by:

sudo apt-get install python-dev python3-dev python-pip python3-pip

Next prepare and register the kernel environments

python -m pip install virtualenv --user

# configure python2 kernel
python -m virtualenv -p python2 ~/py2_kernel
source ~/py2_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py2 --user
deactivate

# configure python3 kernel
python -m virtualenv -p python3 ~/py3_kernel
source ~/py3_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py3 --user
deactivate

To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc

alias kernel2='source ~/py2_kernel/bin/activate'
alias kernel3='source ~/py3_kernel/bin/activate'

After restarting your shell, you can now install new packages after activating the environment you want to use.

kernel2
python -m pip install <pkg-name>
deactivate

or

kernel3
python -m pip install <pkg-name>
deactivate
3
  • followed this to the dot, installed pandas, started jupyter, switched to py3 kernel: import pandas failed with 'not found' error. It's driving me bonkers Commented Mar 12, 2017 at 1:57
  • 1
    @user1255933, this is likely due to a installing with the wrong pip version. This can happen if activating the target environment failed or it does not contain a pip version. You might find my answer here interesting: stackoverflow.com/questions/32680081/….
    – cel
    Commented Mar 12, 2017 at 12:55
  • Thank you for the instruction on using non-anaconda way to install kernel
    – mdivk
    Commented Sep 7, 2018 at 19:35
40

With a current version of the Notebook/Jupyter, you can create a Python3 kernel. After starting a new notebook application from the command line with Python 2 you should see an entry „Python 3“ in the dropdown menu „New“. This gives you a notebook that uses Python 3. So you can have two notebooks side-by-side with different Python versions.

The Details

  1. Create this directory: mkdir -p ~/.ipython/kernels/python3
  2. Create this file ~/.ipython/kernels/python3/kernel.json with this content:

    {
        "display_name": "IPython (Python 3)", 
        "language": "python", 
        "argv": [
            "python3", 
            "-c", "from IPython.kernel.zmq.kernelapp import main; main()", 
            "-f", "{connection_file}"
        ], 
        "codemirror_mode": {
            "version": 2, 
            "name": "ipython"
        }
    }
    
  3. Restart the notebook server.

  4. Select „Python 3“ from the dropdown menu „New“
  5. Work with a Python 3 Notebook
  6. Select „Python 2“ from the dropdown menu „New“
  7. Work with a Python 2 Notebook
8
  • This is great and I am trying to get that working, but could you specify how you start jupyter in this scenario (I either have to run ipython notebook or ipython3 notebook). Right now I can only run one or the other (with their respective environments, but I do see both kernels listed inside jupyter. Could you maybe expand your answer to include how to start jupyter so that I can run python2 and python3 side-by-side? Thanks!
    – Chris
    Commented Aug 27, 2015 at 17:13
  • 1
    Ok, I think I figured it out - I had to adjust the kernel.json file inside ~/Library/Jupyter/kernels/python3/ (on OS X) and add the arguments mentioned in the linked file.
    – Chris
    Commented Aug 27, 2015 at 18:20
  • @Chris Yes, while the description behind the link is given in an IPython Notebook, it could be done in an editor. Just create or modify a file with a certain name at a given path and at the the shown content. Glad that you solved your problem. Commented Aug 29, 2015 at 5:27
  • I would recommend to edit your answer to include the details from that link. Answers shouldn't keep the most important part hidden "behind" a link.
    – Chris
    Commented Aug 29, 2015 at 15:18
  • 1
    @Chris Added the details from the link. Commented Aug 30, 2015 at 8:43
24

A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.

Requirements:

  • MacPorts is installed in the usual /opt directory
  • python 2.7 is installed through macports
  • python 3.4 is installed through macports
  • Ipython is installed for python 2.7
  • Ipython is installed for python 3.4

For python 2.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self

For python 3.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self

Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.

Choose your python!

5
  • Can you please confirm that you can run python2 and python3 kernel notebooks side-by-side in the same jupyter instance? In this case, how exactly do you start jupyter to not have conflicting paths? I currently can only either run python2 or python3 code by setting up the appropriate $PATH $PYTHONPATH environment beforehand. Can I avoid this somehow?
    – Chris
    Commented Aug 27, 2015 at 17:20
  • I can run either python2 or python3 kernel notebooks.
    – deltap
    Commented Aug 27, 2015 at 18:07
  • How do you start jupyter (given that it is installed for python 2.7 and for python 3.4) Have you defined $PYTHONPATH or sourced some virtual environment? Are you just calling ipython notebook ? If so, which ipython is that referring to - the one installed for 2.7 or 3.4?
    – Chris
    Commented Aug 27, 2015 at 18:13
  • I am just calling python notebook. which ipython shows that it is pointing to /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin/ipython
    – deltap
    Commented Aug 28, 2015 at 19:25
  • Alright, I figured out my problem and it had to do with a pre-defined $PYTHONPATH, which makes switching kernels difficult. I had to unset PYTHONPATH before starting ipython and now it works.
    – Chris
    Commented Aug 29, 2015 at 15:19
19

From my Linux installation I did:

sudo ipython2 kernelspec install-self

And now my python 2 is back on the list.

Reference:

http://ipython.readthedocs.org/en/latest/install/kernel_install.html


UPDATE:

The method above is now deprecated and will be dropped in the future. The new method should be:

sudo ipython2 kernel install

6
  • 2
    I hate randomly pasting commands into my Ubuntu install, but this did work for me.
    – Joseph
    Commented Oct 20, 2015 at 18:33
  • 1
    Should be the chosen answer.
    – e9t
    Commented Jan 18, 2016 at 15:26
  • 1
    Brief and the to the point, I've applied this solution on Mac OS X, works with no problems.
    – Konrad
    Commented Apr 14, 2016 at 18:45
  • 1
    I tried all the answers. But this worked for me. Give it a try if you are on ubuntu.
    – sinsuren
    Commented Oct 21, 2016 at 18:30
  • 1
    And where do I get ipython2?
    – sudo
    Commented Apr 22, 2017 at 0:23
6

Following are the steps to add the python2 kernel to jupyter notebook::

open a terminal and create a new python 2 environment: conda create -n py27 python=2.7

activate the environment: Linux source activate py27 or windows activate py27

install the kernel in the env: conda install notebook ipykernel

install the kernel for outside the env: ipython kernel install --user

close the env: source deactivate

Although a late answer hope someone finds it useful :p

1
  • This doesn't add anything beyond what was already clearly stated in @cel's answer.
    – merv
    Commented May 8, 2019 at 13:40
3

Use sudo pip3 install jupyter for installing jupyter for python3 and sudo pip install jupyter for installing jupyter notebook for python2. Then, you can call ipython kernel install command to enable both types of notebook to choose from in jupyter notebook.

1

I looked at this excellent info and then wondered, since

  1. i have python2, python3 and IPython all installed,
  2. i have PyCharm installed,
  3. PyCharm uses IPython for its Python Console,

if PyCharm would use

  1. IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
  2. IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3

ANSWER: Yes!

P.S. i have Python Launcher for Windows installed as well.

0

Under Windows 7 I had anaconda and anaconda3 installed. I went into \Users\me\anaconda\Scripts and executed

sudo .\ipython kernelspec install-self

then I went into \Users\me\anaconda3\Scripts and executed

sudo .\ipython kernel install

(I got jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.)

After starting jupyter notebook (in anaconda3) I got a neat dropdown menu in the upper right corner under "New" letting me choose between Python 2 odr Python 3 kernels.

0
  • If you are running anaconda in virtual environment.
  • And when you create a new notebook but i's not showing to select the virtual environment kernel.
  • Then you have to set it into the ipykernel using the following command
$ pip install --user ipykernel
$ python -m ipykernel install --user --name=test2

Not the answer you're looking for? Browse other questions tagged or ask your own question.