Install Python 3 and Python 2 kernels for Jupyter notebook - lmmx/devnotes GitHub Wiki

After I went through the steps below with miniconda, I uninstalled that and used full Anaconda (Python 3.5 installer). I did this because of an OS-specific bash bug (on Linux Mint and possibly also Ubuntu I don't know). Solving it required setting an environment variable. tl;dr go ahead with the following instructions, you can uninstall Miniconda at any point (just consists of removing a directory and .bashrc line anyway), you will keep all the scientific Python packages it installs along the way.

Installing the full Anaconda3 using Python3.5 takes a quick few minutes.

During the course of this setup, I also made python3 the default python with alias python='python3' in my .bashrc (see note on PEP 394)

NB: the latest IPython is version 4. ipython3 and ipython2 are not to be confused with python2 and python3 - these are the language "kernels".

My understanding of this process: If you have a Python2.7 interpreter as your default, then [pip install'ing] IPython will use a Python2 kernel, on top of which you must install the Py3 kernel. If you have a Python3 installation (as ensured by Conda), you get Python3 and install Python2 kernel in addition. Anaconda handles the difficulties of dependency installation, and the steps below will set Python3 as default python through creation of an "environment".

Installing Python 3 on Linux while retaining Python 2 is possible (as described here):

  • First install conda (miniconda is the bare build necessities rather than 100+ other FOSS packages, installer available as a simple shell script here)

  • bash Miniconda*-latest-Linux-x86_64.sh

    Warning received:

WARNING: You currently have a PYTHONPATH environment variable set. This may cause unexpected behavior when running the Python interpreter in Miniconda3. For best results, please verify that your PYTHONPATH only points to directories of packages that are compatible with the Python interpreter in Miniconda3: /home/louis/Downloads/Miniconda3


* once installed you can use a Conda python3 session to pip install IPython 3, specifying the current version of python (I think this is optional - I changed it from 3.4 on the [Duke guide](http://people.duke.edu/~ccc14/sta-663/Jupyter.html))

   ```sh
conda create -n python3 python=3.5 anaconda

...this will install 100+ packages? Takes ages - numpy, libxslt, lxml etc - but includes pip, required for next step. Click here to see example output.

source activate python3
pip install -U ipython
ipython3 kernelspec install-self

Weird Anaconda/bash bug appeared... was from command_not_found_handle function using Python3

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x00007f78508cf740 (most recent call first):
Aborted

Added to my .bashrc:

export COMMAND_NOT_FOUND_FORCE_PYTHON2 # weird Anaconda-produced bug

Now, restart your notebook server

If you were successful, you should now see a large number of kernel options in the New drop down menu.

Note that you can also change the kernel used for each individual cell!

⚠️ **GitHub.com Fallback** ⚠️