18

I am trying to test some code on Python 3.6 but my system default python version is python 3.5 I have installed python 3.6 and have tried to follow the jupyter documentation to install a new python kernel

python3.6 -m pip install ipykernel
python3.6 -m ipykernel install --user

But it didn't work since it continues to show a single kernel in the menu: Python3

Has anyone managed to have both 3.5 and 3.6 in the same jupyter installation?

fccoelho
  • 6,012
  • 10
  • 55
  • 67

7 Answers7

31

one option is to create a kernel that you can use in jupyter notebook. enter image description here

you can do this inside the virtual environment:

  1. Open up your terminal and enter the following line by line

    virtualenv -p python3.6 py_36_env

    source py_36_env/bin/activate

    pip install ipykernel

    python -m ipykernel install --user --name=py_36_env

    jupyter notebook

  2. Then in jupyter notebook you can select the 3.6 environment (py_36_env) from the 'New' drop down menu shown above or from the 'Kernel' drop down menu within a given jupyter notebook.

Lauren
  • 5,640
  • 1
  • 13
  • 19
  • 3
    What if we don't have virtualenv? – Schütze Dec 10 '18 at 17:37
  • do you not want to use it? or do you not have it? you can just pip install the virtualenv if you don't have it. – Lauren Dec 10 '18 at 18:07
  • No, it only brings complication. I want a single environment only. – Schütze Dec 10 '18 at 18:07
  • ah okay, yeah i'm not sure how to do it without virtualenv, you may want to ask this question as a new post. – Lauren Dec 10 '18 at 20:02
  • 3
    @Schütze virtualenv is not needed. `ipykernel install` registers Python kernel with the Python binary you used to run it. See [Running Jupyter with multiple Python and IPython paths](https://stackoverflow.com/a/39022003/320437). For example this command registers new kernel with Python 3.9: `python3.9 -m ipykernel install --user --name python3.9`. – pabouk - Ukraine stay strong Feb 27 '21 at 22:03
12

Steps to install Python 3.6 in Windows

  1. Open command prompt
  2. conda install ipykernel
  3. conda create -n Python3.6Test python=3.6
  4. Activate Python3.6Test
  5. pip install ipykernel
  6. python -m ipykernel install --name Python3.6Test

Here you go with 3.6 C:\ProgramData\jupyter\kernels\Python3.6Test

Now open Jupitor Notebook and see, you will get the Python3.6Test option

smallbutton
  • 3,377
  • 15
  • 27
user3274034
  • 153
  • 1
  • 11
3

Following worked for me:

Commands are executed in Jupyter Notebook (OS: Ubuntu 16.04 LTS)

Upgrade pip:

!pip install --upgrade pip

Install virtual environment:

!pip install virtualenv

Select version of Python you want to use in new environment:

I wanted to create an environment with Python version 3.6 Naming it as Python_3_6:

!virtualenv -p python3.6 Python_3_6

After execution, this will create a folder with the same name in the current working directory (i.e. the location where Jupyter notebook is present)

Create a new option with the name of the created environment

And finally, run the following command:

!python -m ipykernel install --user --name=Python_3_6

This will create a new option with the name Python_3_6 in the menu from where we create a new notebook.

NOTE: One can run above commands from the terminal as well just don't use '!' before the commands.

Yogesh Awdhut Gadade
  • 2,498
  • 24
  • 19
3

This is an old question, but like some commenters here I wanted to change the default jupyter completely without using a venv. It seems jupyter keeps this in a json file called kernel.json:

{
 "display_name": "Python 3",
 "language": "python",
 "argv": [
  "/path/to/some/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ]
}

There may be several paths containing this file which are searched in order. To find out what they are on your machine, run:

ipython kernel install --prefix tmp

ipython will throw a warning that the config file is being created in tmp and might not be found, but it will also output the paths it uses to find kernel.json, which you can then edit manually.

azeldes
  • 183
  • 1
  • 7
2

You should create a virtualenv for each Python version you are using. Create one for Python 3.5:

virtualenv -p /usr/bin/python3.5 py35
source py35/bin/activate
pip install jupyter
jupyter               # Jupyter running Python 3.5
deactivate           # Leave virtualenv

And create one for Python 3.6:

virtualenv -p /usr/bin/python3.6 py36
source py36/bin/activate
pip install jupyter
jupyter               # Jupyter running Python 3.6
fccoelho
  • 6,012
  • 10
  • 55
  • 67
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • I have tried that, and even though jupyter started with the correct version of python, python 3.6 could not (find) import packages I had installed in the virtualenv. – fccoelho May 04 '17 at 13:19
  • You need to install all other packages too – Nils Werner May 04 '17 at 13:20
  • Sure, forgot to say that the virtualenv is fine, I can run ipython and import the libraries i installed. it was only in the notebook that it could not import anything. So I think this may be an issue with the sys.path in the jupyter-notebook. – fccoelho May 05 '17 at 15:47
  • How do you start the notebook? And do you change `sys.path` in the notebook? – Nils Werner May 06 '17 at 08:08
1
  • upper solution does not work in my case!!
  • I want to execute code in python3.8 but unfortunately I previous install pytohn3.6
  • I create, activate virtualenv of python3.8 but during lib installation all package installed in virtualenv of python3.6
  • Solution is that I deleted python3.6 env & Setup a new way of Jupyter kernel. After that mine code execute happily & required lib install in virtualenv of python3.8
Ashish Sondagar
  • 917
  • 1
  • 9
  • 16
0

For Windows users:

To add python 3.x ipykernel to jupyter notebook with name "Python 3.x", shown as 'Python 3.x', open your terminal (such as Anaconda Prompt), in base env and enter the following line by line.

For Python 3.10 ipykernel:

conda create -n py310 python=3.10 ipykernel
conda activate py310
python -m ipykernel install --user --name=py310 --display-name "Python 3.10"

For Python 3.7.7 ipykernel:

conda create -n py377 python=3.7.7 ipykernel
conda activate py377
python -m ipykernel install --user --name=py377 --display-name "Python 3.7"

Then close the specific env and go back to the base env, and type in jupyter notebook. Once it opens you'll see the following when you click the "New" drop down menu in the top right corner:

enter image description here

Then to check if the correct Python is installed, open a new notebook and run:

from platform import python_version
print(python_version())

For Python 3.10:

enter image description here

For Python 3.7.7

enter image description here

To remove a specific kernel, you can run the following in your terminal in the base env:

jupyter kernelspec uninstall <kernel name>