简体   繁体   中英

iPython kernels and Anaconda environments

I wanted to install both Python 2 and Python 3 kernels for my Jupyter notebook, so I followed the instructions here http://ipython.readthedocs.io/en/stable/install/kernel_install.html using conda. I initially had only Python 3 kernel running, so I installed Python 2 with:

conda create -n ipykernel_py2 python=2 ipykernel
source activate ipykernel_py2  
python -m ipykernel install --user

Now I have a new environment ipykernel_py2 on Anaconda, and I have Python 2 kernel available on Jupyter. However, I realised that even on the original base (root) environment, I still have Python 2 available. Now I am a little confused, as I recall this is installing a new conda environment, and I should only have access to the Python 2 kernel when in the ipykernel_py2 environment.

When I use the Python 2 kernel in Jupyter, it acts as if it is in its own environment: the packages like pandas/matplotlib I installed in the original base(root) environment are not installed in the Python 2 kernel environment.

Could someone explain to me, why the Python 2 kernel is accessible even outside the ipykernel_py2 environment? Has it got something to do with the kernel spec file that is installed? (mentioned on the link).

You don't install ipython kernel with python -m ipykernel install --user . That command, in fact, installs the kernel spec for the corresponding kernel. The kernel itself is installed from the ipykernel package. You did it during environment creation: conda create -n ipykernel_py2 python=2 ipykernel .

The ipython kernel might be located in the environment, which is different from your Jupyter environment. That's absolutely normal. When you select it in Jupyter, the kernel spec will point to the kernel in a different environment, and it will be executed by the interpreter in that target environment. The execution context will be the one of the environment with the kernel, not of the environment with Jupyter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM