简体   繁体   中英

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

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"}
}

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.

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