How to: Installing Multiple Python Kernels or Version in System

Post a Comment

If you are reading this, then I guess you might be a multi-facet geek who likes jumping among python versions. Moving On...

The possible way of Installing multiple versions of python, having different versions of Python MSI installers and installing them one by one. That depends, which one you install last will become the default (meaning: On python command at shell will launch that last installed python.) This case happens sometimes, which I've faced.

A Cleaner Approach would be to create virtual environments using conda package management in Anaconda. Because with it we can list out how many virtual environments (venvs ) a system actually has rather than creating and finding where a particular environment was created.
Anaconda comes in two flavors: mini-conda and anaconda with too many packages (heavy in size).

Beginners: Go with Anaconda with many packages.
Here: https://www.anaconda.com/download/

People who know what modules they specifically want: Go with MiniConda.
Here:  https://conda.io/miniconda.html

Now, I'm going to talk about anaconda with many Packages. It comes with different versions of Python in it when you install. You can see different version by command

$: conda info python 3.6 or conda info python 2.7  (depending on which full-fledged anaconda with python you've installed)

for above code to be run, either path of conda must be added in PATH or command should be run on anaconda-console. (found in anaconda-navigator)

Now that you've seen that you have different versions of python with your anaconda. It's time to use them and switch among them as per need. The Switching can be done visually (GUI) using jupyter notebook which uses a concept of Kernel (a backend support of running code.). The jupyter notebook has default kernel of python version which you've installed anaconda with. for example, you've installed Anaconda with python 3.6 then jupyter notebook will have default kernel as Python 3.

Adding a New Kernel in Jupyter Notebook:
Below commands are for Linux, simply remove 'source' word from them to run in windows.

  • Creating a kernel of python version 2.7          
  • $: conda create -n py27env python2.7      ---- creating env with name py2.7env & python2.7
    $: source activate py27env                         ---- going into environment
    $: python -m pip install ipykernel             ----  installing kernel
    $: python -m ipykernel install --user        ---- registering kernel for jupyter

  • Creating a kernel of python version 3.4          
  • $: conda create -n py34env python=3.4    ---- creating env with name py34env & python3.4
    $: source activate py34env                         ---- going into environment
    $: python -m pip install ipykernel             ----  installing kernel
    $: python -m ipykernel install --user        ---- registering kernel for jupyter

Now, Getting out of environments with

         $: source deactivate

On Loading up a jupyter notebook from terminal or cmd:

$: jupyter notebook

Under menu Kernel > change kernel: a list of kernels will be available which we require and created.

The key thing to Remember: When you switch to a python kernel of a particular version, they are actually switched to virtual environments inside which they were created. When in a case, the need of module happens, for a particular python version, then manually module has to be installed for that virtual env that kernel uses.

Thats all !

Do Share your thoughts. Share article. Share your issues if occurs.
#keeponhacking.



Y Aakash
Hello World, I Dwell with Creative Sketching, Coding, Finance & Blogging by putting my Views and Work.

Related Posts

Post a Comment