Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anaconda: Python 3 and 2 in IPython/Jupyter Notebook

I have a Python 3 installation of Anaconda and want to be able to switch quickly between python2 and 3 kernels. This is on OSX.

My steps so far involved:

conda create -p ~/anaconda/envs/python2 python=2.7
source activate python2
conda install ipython
ipython kernelspec install-self
source deactivate

After this I have a python2 Kernel to choose from in the python3 IPython notebook, which however can't start.

So I went ahead and modified /usr/local/share/jupyter/kernels/python2/kernel.json

{
 "display_name": "Python 2",
 "language": "python",
 "argv": [
  "/Users/sonium/anaconda/envs/python2/bin/python",
  "-m",
  "IPython.kernel",
  "-f",
  "{connection_file}"
 ],
 "env":{"PYTHONHOME":"~/anaconda/envs/python2/:~/anaconda/envs/python2/lib/"}
}

Now when I start the python2 kernel it fails with:

ImportError: No module named site
like image 651
sonium Avatar asked Apr 15 '15 10:04

sonium


2 Answers

Apparently IPython expects explicit pathnames, so no '~' instead of the home directory. It worked after changing the kernel.json to:

{
 "display_name": "Python 2",
 "language": "python",
 "argv": [
  "/Users/sonium/anaconda/envs/python2/bin/python2.7",
  "-m",
  "IPython.kernel",
  "-f",
  "{connection_file}"
 ],
 "env":{"PYTHONHOME":"/Users/sonium/anaconda/envs/python2"}
}
like image 196
sonium Avatar answered Nov 06 '22 14:11

sonium


I install the Anaconda 3 in Win10. I am now focus on python 3, but I have lot projects written in python 2. If I want check them in juypter in python environment it's will failed, and shows "kernel error". The solution is almost like above, but something different.

The path to find those two json files is : C:\ProgramData\jupyter\kernels Sometime it may be hidden.

Another path you need check, after you create a python2 environment in Anaconda, try to find this path: C:\Users\username\Anaconda3\envs\python2\python.exe

Copy it to your python2 kernel json file then it should be work.

like image 39
Wolf Avatar answered Nov 06 '22 12:11

Wolf