anaconda / nb_conda_kernels

Package for managing conda environment-based kernels inside of Jupyter
BSD 3-Clause "New" or "Revised" License
601 stars 70 forks source link

How to Change Default kernel in jupyter notebook #162

Closed ankitgupta1729 closed 4 years ago

ankitgupta1729 commented 4 years ago

I am using ipython 6.4.0 on ubuntu 20.04 and using jupyter kernelspec list , I found, there are 2 kernels :

  1. practice_applied_ai
  2. python3

When I open any .ipynb file, it directly opens in "python3" but I want to open it in "practice_applied_ai" because I created virtual environment practice_applied_ai and only in this kernel I can import Tensorflow 2.2.0 for my work.
My question is, Is there any way to change my default kernel without removing any kernel ?

kevin-bates commented 4 years ago

Hi @ankitgupta1729 - This config option (CLI version shown here) should do the trick:

--MappingKernelManager.default_kernel_name=practice_applied_ai
ankitgupta1729 commented 4 years ago

Hi @ankitgupta1729 - This config option (CLI version shown here) should do the trick:

--MappingKernelManager.default_kernel_name=practice_applied_ai

@kevin-bates, Thank you for the reply. I have put "MappingKernelManager.default_kernel_name=practice_applied_ai" in the "jupyter_notebook_config.py" file but still it opens a ipynb file in "python3". Should I have to do something else ?

kevin-bates commented 4 years ago

Got a couple of questions first.

ankitgupta1729 commented 4 years ago

Hello sir, I have manually edited the jupyter_notebook_config.py by creating this file using command jupyter notebook --generate-config in .jupyter folder from terminal and then opened it using gedit and then uncommented and saved this file. Sorry if I have done wrong. I have tried jupyter notebook --MappingKernelManager.default_kernel_name=practice_applied_ai and ipython --MappingKernelManager.default_kernel_name=practice_applied_ai but these are not working and in jupyter_notebook_config.py file, I saw only "c.MultiKernelManager.default_kernel_name" entry and when I have changed this attribute's value to "practice_applied_ai", nothing happened. So, I edited that file manually.
I open jupyter notebook in browser by running command jupyter notebook in terminal and when I open any file from that, it opens it in python3 kernel but I want to open it in "practice_applied_ai" kernel though I can change the kernel (there is an option there) but I have to do it everytime.

kevin-bates commented 4 years ago

Sounds like you're doing the right thing.

when I open any file from that, it opens it in python3 kernel

If you're opening existing notebook files, the kernel-name is part of the file's metadata - so it will use that kernel. I'm not sure how to trigger the use of the "default kernel" other than from the REST API I described previously. The Notebook and Lab UIs require the user to select a kernel when creating notebooks and that selection is from the list of available kernels - which includes the default kernel (among others). You might have the wrong idea about when the default kernel name is used.

Another way to determine your configuration of a default kernel is working is to visit this URL via a web browser: http://localhost:8888/api/kernelspecs. This is much easier than issuing a POST request as I described previously.

The response to visiting that URL is the set of kernelspec information. The first portion of which is the name of the default kernel. Again, you'll see here the the 'ir' kernel is (now) configured as my default:

{
  "default": "ir",
  "kernelspecs": {
    "python3": {
      "name": "python3",
      "spec": {
        "argv": [
          "/opt/anaconda3/envs/elyra-dev/bin/python",
          "-m",
          "ipykernel_launcher",
          "-f",
          "{connection_file}"
        ],
        "env": {},
        "display_name": "Python 3",
        "language": "python",
        "interrupt_mode": "signal",
        "metadata": {}
      },
      "resources": {
        "logo-64x64": "/kernelspecs/python3/logo-64x64.png",
        "logo-32x32": "/kernelspecs/python3/logo-32x32.png"
      }
    },
    "ir": {
      "name": "ir",
      "spec": {
        "argv": [
          "/Library/Frameworks/R.framework/Resources/bin/R",
          "--slave",
          "-e",
          "IRkernel::main()",
          "--args",
          "{connection_file}"
        ],
        "env": {},
        "display_name": "R",
        "language": "R",
        "interrupt_mode": "signal",
        "metadata": {}
      },
      "resources": {
        "kernel.js": "/kernelspecs/ir/kernel.js",
        "logo-64x64": "/kernelspecs/ir/logo-64x64.png"
      }
    }
  }
}
ankitgupta1729 commented 4 years ago

Sir, Thank you so much for the help :)

If you're opening existing notebook files, the kernel-name is part of the file's metadata - so it will use that kernel.

Yes, I checked now. I thought I can open other files which are used with different kernels in the kernel "practice_applied_ai". I did not know that kernel name is part of file's metadata. Now, I understood it.

kevin-bates commented 4 years ago

Glad to hear you're moving forward. I think this issue can be closed.

ankitgupta1729 commented 4 years ago

yes sir, I am closing it now.