Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place custom Jupyter kernels inside virtual environment?

I have a custom Jupyter kernel which runs IPython using a custom IPython profile which uses a matplotlib stylesheet.

I know to run this successfully normally I would put:

  • The matplotlib stylesheet in ~/.config/matplotlib/stylelib/

  • The IPython profile in ~/.ipython/

  • The kernel json in ~/.jupyter/kernels/my_kernel/

But I am doing this as part of larger program which runs in a virtualenv, and if I put the things as above then any notebook server running on the computer will be able to see the custom kernels, even if it is running outside the venv. I don't what this because I don't want my program to interfere with other notebooks on the computer.

I think what I need to do is put the things above somewhere equivalent inside the venv but I can't figure out where they should go. Doe anyone know where they would go? Or is this just a thing IPython/Jupiter can't/won't do?

It's probably worth mentioning that in the case of the stylesheet for example I don't want to just put it in the working directory of my program (which is one option matplotlib offers).

like image 417
C. dBD Avatar asked Feb 09 '16 01:02

C. dBD


People also ask

How do I add kernel to Jupyter notebook VS code?

To run the remote kernel in VSCode, first install the Jupyter extension from the extensions panel. From the command palette , search for Specify local or remote Jupyter server for connections . Lastly, choose an Existing kernel and paste the URI from Gradient.

How do I change the kernel path in Jupyter notebook?

To select the kernel in a CoCalc Jupyter notebook, click the “Kernel” button (usually in the middle toolbar, depending on your configuration). In the menu that opens, scroll down past interrupt and restart commands to see the choices for available kernels.


1 Answers

You can put kernelspecs in VIRTUAL_ENV/share/jupyter/kernels/ and they will be made available if the notebook server is running in that env. In general, <sys.prefix>/share/jupyter/kernels is included in the path to look for kernelspecs.

You can see the various locations Jupyter will look, you can see the output of jupyter --paths:

$ jupyter --paths

config:
    /Users/you/.jupyter
    /Users/you/env/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /Users/you/Library/Jupyter
    /Users/you/env/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /Users/you/Library/Jupyter/runtime

Kernelspecs are considered data files, and will be found in any of those directories listed under data:, in a kernels subdirectory, e.g. /usr/local/share/jupyter/kernels.

like image 70
minrk Avatar answered Sep 25 '22 18:09

minrk