By deltap


2015-05-27 20:34:41 8 Comments

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.

I initially had Anaconda. With Anaconda a global environment variable had to be changed to select what version of python you want and then IPython could be started. This is not what I was looking for so I uninstalled Anaconda and now have set up my own installation using MacPorts and PiP. It seems that I still have to use

port select --set python <python version> 

to toggle between python 2.x and 3.x. which is no better than the anaconda solution.

Is there a way to select what version of python you want to use after you start an IPython notebook, preferably with my current MacPorts build?

10 comments

@cel 2015-05-27 20:52:56

The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

Anaconda >= 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

conda create -n py27 python=2.7 ipykernel
conda create -n py36 python=3.6 ipykernel

After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new environments. The Managing environments section in conda's docs provides further information.

Manually registering kernels

Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

configure the python2.7 environment:

conda create -n py27 python=2.7
conda activate py27
conda install notebook ipykernel
ipython kernel install --user

configure the python3.6 environment:

conda create -n py36 python=3.6
conda activate py36
conda install notebook ipykernel
ipython kernel install --user

After that you should be able to choose between python2
and python3 when creating a new notebook in the interface.

Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.

@deltap 2015-05-27 21:33:01

Your solution looks a lot like the solution I ended up using and in principle I would expect it to work. Since I already uninstalled anaconda and got this working I won't be able to verify.

@Mike Müller 2015-05-27 21:45:05

Works on Mac. The new kernel specifications end up in /usr/local/share/jupyter/kernels/kernel2/kernel.json and /usr/local/share/jupyter/kernels/kernel3/kernel.json.

@cel 2015-05-27 21:58:45

@deltap, yes our solutions are very similar. The only difference is that your solution requires root access because it does not use a virtualenv. I personally prefer virtualenvs, but this is a matter of taste I guess. If it works, everything is fine :)

@cel 2015-05-27 22:01:39

@MikeMüller, well good for all searching for this question - but bad for me. I really can't figure out why it does not work for me :)

@Thomas K 2015-05-28 03:10:55

If you want to set up the kernelspecs without requiring root, you can do ipython kernelspec install-self --user to install them for the current user.

@Clever Programmer 2015-08-25 03:13:07

This solution was amazing. Thank you. I got both my ipython notebooks up and running now. However, is there a way when I click the [NEW] button in IPython Notebook, it gives me two options: 1. Python 2 and 2. Python 3? Is that a possibility to have both of the options show in one place? @cel

@cel 2015-08-25 10:40:39

@Rafeh, can you try to rephrase your question? I am having trouble understanding what "both of the options in one place" refers to.

@Clever Programmer 2015-08-25 10:42:24

I have figured it out! Your answer helped out! dropbox.com/s/6ayqf55ctkv2xgk/…

@Clever Programmer 2015-08-25 23:38:26

Now when I click [NEW NOTEBOOK] button, in the drop-down menu it shows both, python 2 & python 3. It only works if I am running the ipython notebook through my main installation of Anaconda (not the virtual envs).

@Chris 2015-08-27 17:16:28

Anybody figured out how I need to start jupyter, when I want to run python 2 & 3 side-by-side like @Rafeh mentioned, but I do not want to use Anaconda?

@cel 2015-08-27 17:34:45

@Chris, you can do the same steps but replace the anaconda with simple virtualenvs.

@Chris 2015-08-27 18:03:56

@cel thanks, yes, I can see the two kernels inside jupyter, but I can only run one of them, depending on which env I start jupyter with. However, there should be a way that I can run two notebooks side-by-side, one with the python2 kernel and the other one with python3. I just don't know how to accomplish that. Currently, if I start jupyter from a python2 env, the python3 notebook opens, but the kernel crashes and the same the other way around. Any ideas what I am doing wrong? Probably related to my $PATH $PYTHONPATH setup?

@cel 2015-08-27 18:18:23

@Chris, that will be too complicated to figure out in the comments. You can instead ask a question and include as much detail as possible.

@Chris 2015-08-27 18:44:16

@cel thanks, I was about to do that, but I just figured it out the problem and as it turns out - the answer below by @MikeMüller did help me (spoiler: I had to edit the kernel.json file created by the ipython kernelspec install-self according to the link he provided)

@pylang 2015-11-21 03:34:28

Using these instructions on Win8.1, neither anaconda nor ipython was installed in the venv. This was resolved by removing the venv and reinstalling by adding anaconda to the end, i.e. conda create -n py27 python=2.7 anaconda`

@cel 2015-11-21 06:55:37

@pylang, note that there is no need to remove environments in this case. After activating the environment of your choice, you can add additional packages using conda install <package-name>.

@pylang 2015-11-21 16:21:37

@cel, thanks. I was unsure if anaconda would link to existing packages. Also, an edit to my former comment: "...anaconda to the end, i.e. conda create -n py27 python=2.7 anaconda"

@Romain 2016-02-04 18:13:20

great answer thanks for that ! You literally saved me.

@leon yin 2016-05-16 23:12:11

My root environment is 3.5 and I created a new environment called py35 like above. I lost my pip installed packages, am I able to have this new environment point to the root bin?

@cel 2016-05-17 04:56:06

@leonyin, if you want to use the root environment in jupyter, you can install another ipython kernel into the root environment. Just do conda install notebook ipykernel and ipython kernel install --user --name root_py3 in your root enviroment and choose the root_py3 kernel in the notebook.

@minibuffer 2016-07-04 06:34:33

Nice. It works.

@dartdog 2016-08-02 17:34:50

Using Ubuntu I installed Anaconda then followed the instructions to do conda create -n py27 python=2.7 ipykernel conda create -n py35 python=3.5 ipykernel and when I restart the notebook I still only get the python 3 option on the notebook? How can I enable python 2 as a choice for new notebooks?

@cel 2016-08-02 17:37:02

@dartdog, as described in the answer there are two cases: Either you have nb_conda_kernel, then this suffices. Else you have to install the kernel manually as described in the second half of the answer.

@dartdog 2016-08-02 18:16:04

The key item is that you must INSTALL nb_conda_kernels it did not come with my initial Anaconda! Thanks!

@dartdog 2016-08-02 18:24:00

So now I have multiple Conda env's (for p2 and p3) is there an easy way to install all the base anaconda packages in both? In something like a universal requirements text file for scikit learn etal?

@cel 2016-08-02 21:11:48

@dartdog, anaconda is a (meta-)package and you can install it as any other package. conda install anaconda.

@Matt Kleinsmith 2017-04-06 15:30:10

Installing nb_conda_kernels: conda install nb_conda_kernels

@The Red Pea 2017-05-07 21:04:35

In case anyone runs across jupyter_environment_kernels; apparently nb_conda_kernels and jupyter_environment_kernels have a similar goal, and maybe even some common code, as discussed by their creators

@Lyle 2017-06-14 10:49:19

installing kernelspec by "ipython kernel install --user" was missing one that I need after adding conda env with a new python version.

@Antoine Gautier 2017-09-25 15:33:32

With Anaconda 4.4 it seems you need to install ipykernel manually as discussed here stackoverflow.com/a/44786736/3416205

@cel 2017-09-26 05:46:10

@AntoineGautier, I am very surprised to hear this. For me this way still seems to work. Are you sure that the environment you start jupyter from has the package nb_conda_kernels? I didn't find any changes upstream that render this approach unusable.

@Antoine Gautier 2017-09-27 13:42:30

@cel, indeed the environment I start jupyter from does not have the package nb_conda_kernels installed by default. I did not take the time to read all the comments: maybe you could edit your answer and include dartdog's remak.

@Steve 2018-06-23 19:04:26

The auto instructions here didn't work me me but the manual ones did! Thanks!

@mdivk 2018-10-10 13:38:22

Not working for me, when I do source activate py36, it errors out: -bash: /home/jhelmus/workspace/misc/cdh_parcel/anaconda/scripts/par‌​cel/tmp/Anaconda-5.1‌​.0/etc/profile.d/con‌​da.sh: No such file or directory

@cel 2018-10-10 14:08:15

@mdivk, does conda activate py36 work? It could be that I have to update this for the latest version of conda.

@mdivk 2018-10-10 18:35:02

No it doesn't work, it's obviously something leftover in the error: jhelmus, who is most likely the developer of the code. and yes the right command should be conda activate

@cel 2018-10-11 07:54:23

@mdivik, I agree with you that it seems to be some leftover of developer code, however I don't know how to fix it. I guess exporting your environments and reinstalling anaconda with the latest official installer will fix it.

@Akash Chandra 2017-12-19 09:41:45

Following are the steps to add the python2 kernel to jupyter notebook::

open a terminal and create a new python 2 environment: conda create -n py27 python=2.7

activate the environment: Linux source activate py27 or windows activate py27

install the kernel in the env: conda install notebook ipykernel

install the kernel for outside the env: ipython kernel install --user

close the env: source deactivate

Although a late answer hope someone finds it useful :p

@sv_jan5 2017-03-30 13:12:04

Use sudo pip3 install jupyter for installing jupyter for python3 and sudo pip install jupyter for installing jupyter notebook for python2. Then, you can call ipython kernel install command to enable both types of notebook to choose from in jupyter notebook.

@mimoralea 2015-10-08 13:01:41

From my Linux installation I did:

sudo ipython2 kernelspec install-self

And now my python 2 is back on the list.

Reference:

http://ipython.readthedocs.org/en/latest/install/kernel_install.html


UPDATE:

The method above is now deprecated and will be dropped in the future. The new method should be:

sudo ipython2 kernel install

@Joseph 2015-10-20 18:33:02

I hate randomly pasting commands into my Ubuntu install, but this did work for me.

@mimoralea 2015-10-21 12:19:02

I hear you! I just added the documentation reference where I found it.

@e9t 2016-01-18 15:26:35

Should be the chosen answer.

@Konrad 2016-04-14 18:45:41

Brief and the to the point, I've applied this solution on Mac OS X, works with no problems.

@sinsuren 2016-10-21 18:30:20

I tried all the answers. But this worked for me. Give it a try if you are on ubuntu.

@sudo 2017-04-22 00:23:20

And where do I get ipython2?

@Krischu 2016-07-07 08:46:12

Under Windows 7 I had anaconda and anaconda3 installed. I went into \Users\me\anaconda\Scripts and executed

sudo .\ipython kernelspec install-self

then I went into \Users\me\anaconda3\Scripts and executed

sudo .\ipython kernel install

(I got jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.)

After starting jupyter notebook (in anaconda3) I got a neat dropdown menu in the upper right corner under "New" letting me choose between Python 2 odr Python 3 kernels.

@Ish 2016-06-16 11:14:49

If you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this:

python2 -m pip install ipykernel

python2 -m ipykernel install --user

http://ipython.readthedocs.io/en/stable/install/kernel_install.html

@wordsforthewise 2016-07-30 09:07:05

Switch the '2' for '3' if you have python2 already setup and need python3. I don't understand why this isn't the top answer, it definitely wins by Occam's razor and it worked for me.

@Daniel Jung 2016-09-05 15:42:26

I agree. This worked perfectly!

@JSmyth 2016-12-03 04:36:49

Works perfectly. Should be the first answer indeed.

@The Red Pea 2017-05-07 19:44:24

The accepted answer appears to reference the same help as this answer. I want to understand the difference. I notice the accepted answer uses conda command, whereas this answer uses python2 command; moreover the accepted answer uses conda create , which creates a new conda environment; is that right? Both techniques end in python2 -m ipykernel install --user, which is described as "The last command installs a kernel spec file..."

@verystrongjoe 2017-07-13 16:44:31

python2 command is included in python itself? Please give some explanation of this answer elaborately. :)

@Rutger Hofste 2017-07-21 01:16:27

this works, but it is not linked to my system python 2 with additional packages. Is there a way to link to an existing python binary/executable?

@Harry M 2017-09-07 22:42:48

this should be the first answer! The current first answer didn't work for me.

@Pramod Solanky 2017-09-16 06:08:45

That just worked! Thanks

@Alex Punnen 2017-11-15 08:32:35

works , but in ubtuntu 16.04 has to install 1) sudo -E apt-get install python-dev; then in the virtual environment 2) pip2 install ipykernel then 3)python2 -m pip2 install ipykernel

@Matthias 2018-03-27 14:00:33

Running the first line, I get this error: Failed building wheel for scandir ... Command "/usr/local/opt/python/bin/python2.7 -u -c "import setuptool .... Any idea?

@Matthias 2018-03-27 14:08:43

Solved it. Since I'm on MacOS, I had to fun 'xcode-select --install' first.

@Lucien 2018-06-15 14:40:06

Works perfectly in win10, just replace python2 with your\path\to\python(2).exe.

@Marc 2018-12-08 21:11:55

I'm using Conda and this solution works with my current conda environment as desired. Thank You!

@Love and peace - Joe Codeswell 2016-06-13 15:51:47

I looked at this excellent info and then wondered, since

  1. i have python2, python3 and IPython all installed,
  2. i have PyCharm installed,
  3. PyCharm uses IPython for its Python Console,

if PyCharm would use

  1. IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
  2. IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3

ANSWER: Yes!

P.S. i have Python Launcher for Windows installed as well.

@cel 2015-12-25 15:18:22

These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.

I assume that you already have jupyter notebook installed.


First make sure that you have a python2 and a python3 interpreter with pip available.

On ubuntu you would install these by:

sudo apt-get install python-dev python3-dev python-pip python3-pip

Next prepare and register the kernel environments

python -m pip install virtualenv --user

# configure python2 kernel
python -m virtualenv -p python2 ~/py2_kernel
source ~/py2_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py2 --user
deactivate

# configure python3 kernel
python -m virtualenv -p python3 ~/py3_kernel
source ~/py3_kernel/bin/activate
python -m pip install ipykernel
ipython kernel install --name py3 --user
deactivate

To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc

alias kernel2='source ~/py2_kernel/bin/activate'
alias kernel3='source ~/py3_kernel/bin/activate'

After restarting your shell, you can now install new packages after activating the environment you want to use.

kernel2
python -m pip install <pkg-name>
deactivate

or

kernel3
python -m pip install <pkg-name>
deactivate

@user1255933 2017-03-12 01:57:21

followed this to the dot, installed pandas, started jupyter, switched to py3 kernel: import pandas failed with 'not found' error. It's driving me bonkers

@cel 2017-03-12 12:55:19

@user1255933, this is likely due to a installing with the wrong pip version. This can happen if activating the target environment failed or it does not contain a pip version. You might find my answer here interesting: stackoverflow.com/questions/32680081/….

@mdivk 2018-09-07 19:35:47

Thank you for the instruction on using non-anaconda way to install kernel

@Mike Müller 2015-05-27 21:06:46

With a current version of the Notebook/Jupyter, you can create a Python3 kernel. After starting a new notebook application from the command line with Python 2 you should see an entry „Python 3“ in the dropdown menu „New“. This gives you a notebook that uses Python 3. So you can have two notebooks side-by-side with different Python versions.

The Details

  1. Create this directory: mkdir -p ~/.ipython/kernels/python3
  2. Create this file ~/.ipython/kernels/python3/kernel.json with this content:

    {
        "display_name": "IPython (Python 3)", 
        "language": "python", 
        "argv": [
            "python3", 
            "-c", "from IPython.kernel.zmq.kernelapp import main; main()", 
            "-f", "{connection_file}"
        ], 
        "codemirror_mode": {
            "version": 2, 
            "name": "ipython"
        }
    }
    
  3. Restart the notebook server.

  4. Select „Python 3“ from the dropdown menu „New“
  5. Work with a Python 3 Notebook
  6. Select „Python 2“ from the dropdown menu „New“
  7. Work with a Python 2 Notebook

@Chris 2015-08-27 17:13:18

This is great and I am trying to get that working, but could you specify how you start jupyter in this scenario (I either have to run ipython notebook or ipython3 notebook). Right now I can only run one or the other (with their respective environments, but I do see both kernels listed inside jupyter. Could you maybe expand your answer to include how to start jupyter so that I can run python2 and python3 side-by-side? Thanks!

@Chris 2015-08-27 18:20:45

Ok, I think I figured it out - I had to adjust the kernel.json file inside ~/Library/Jupyter/kernels/python3/ (on OS X) and add the arguments mentioned in the linked file.

@Mike Müller 2015-08-29 05:27:17

@Chris Yes, while the description behind the link is given in an IPython Notebook, it could be done in an editor. Just create or modify a file with a certain name at a given path and at the the shown content. Glad that you solved your problem.

@Chris 2015-08-29 15:18:15

I would recommend to edit your answer to include the details from that link. Answers shouldn't keep the most important part hidden "behind" a link.

@Mike Müller 2015-08-30 08:43:57

@Chris Added the details from the link.

@Paul Jurczak 2015-10-27 09:04:13

Very elegant solution, thank you. Worked for me on Kubuntu 15.10. The kernel shows as IPython (Python 3) instead of Python 3, though.

@Makc 2016-06-26 11:29:01

finally you can run "jupyter kernelspec install ~/.ipython/kernels/python3/ --user" It will install kernel to ~/.local/share/jupyter/kernels directory. I'm not sure, but maybe that will help avoid compatibility issues in the feature when Jupyter only will be supported! Ubuntu 14.04 x86_64

@Ivelin 2016-11-08 02:21:01

This gives me kernel error

@user1255933 2017-03-12 02:13:24

IPython.kernel is throwing a deprecation error. This worked for me: "argv": ["python3","-m", "ipykernel","-f", "{connection_file}"]

@deltap 2015-05-27 21:00:39

A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.

Requirements:

  • MacPorts is installed in the usual /opt directory
  • python 2.7 is installed through macports
  • python 3.4 is installed through macports
  • Ipython is installed for python 2.7
  • Ipython is installed for python 3.4

For python 2.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
$ sudo ./ipython kernelspec install-self

For python 3.x:

$ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
$ sudo ./ipython kernelspec install-self

Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.

Choose your python!

@Chris 2015-08-27 17:20:01

Can you please confirm that you can run python2 and python3 kernel notebooks side-by-side in the same jupyter instance? In this case, how exactly do you start jupyter to not have conflicting paths? I currently can only either run python2 or python3 code by setting up the appropriate $PATH $PYTHONPATH environment beforehand. Can I avoid this somehow?

@deltap 2015-08-27 18:07:03

I can run either python2 or python3 kernel notebooks.

@Chris 2015-08-27 18:13:41

How do you start jupyter (given that it is installed for python 2.7 and for python 3.4) Have you defined $PYTHONPATH or sourced some virtual environment? Are you just calling ipython notebook ? If so, which ipython is that referring to - the one installed for 2.7 or 3.4?

@deltap 2015-08-28 19:25:11

I am just calling python notebook. which ipython shows that it is pointing to /opt/local/Library/Frameworks/Python.framework/Versions/3.4/‌​bin/ipython

@Chris 2015-08-29 15:19:52

Alright, I figured out my problem and it had to do with a pre-defined $PYTHONPATH, which makes switching kernels difficult. I had to unset PYTHONPATH before starting ipython and now it works.

Related Questions

Sponsored Content

26 Answered Questions

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

18 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

55 Answered Questions

[SOLVED] Calling an external command in Python

18 Answered Questions

[SOLVED] Using IPython notebooks under version control

26 Answered Questions

[SOLVED] Difference between append vs. extend list methods in Python

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3068248 View
  • 2535 Score
  • 28 Answer
  • Tags:   python list

23 Answered Questions

[SOLVED] What is the difference between @staticmethod and @classmethod?

15 Answered Questions

[SOLVED] What are metaclasses in Python?

10 Answered Questions

[SOLVED] How to make IPython notebook matplotlib plot inline

Sponsored Content