How to place custom Jupyter kernels inside virtual environment?

蓝咒 提交于 2019-12-07 06:44:42

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/35282376/how-to-place-custom-jupyter-kernels-inside-virtual-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!