Using both Python 2.x and Python 3.x in IPython Notebook Using both Python 2.x and Python 3.x in IPython Notebook python python

Using both Python 2.x and Python 3.x in IPython Notebook


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 ipykernelconda 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.7conda activate py27conda install notebook ipykernelipython kernel install --user

configure the python3.6 environment:

conda create -n py36 python=3.6conda activate py36conda install notebook ipykernelipython 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.


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

python2 -m pip install ipykernelpython2 -m ipykernel install --user

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


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 kernelpython -m virtualenv -p python2 ~/py2_kernelsource ~/py2_kernel/bin/activatepython -m pip install ipykernelipython kernel install --name py2 --userdeactivate# configure python3 kernelpython -m virtualenv -p python3 ~/py3_kernelsource ~/py3_kernel/bin/activatepython -m pip install ipykernelipython kernel install --name py3 --userdeactivate

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.

kernel2python -m pip install <pkg-name>deactivate

or

kernel3python -m pip install <pkg-name>deactivate


matomo