conda install jupycon/label/dev::nb_conda_kernels |
|
conda install defaults::nb_conda_kernels |
|
conda install conda-forge::nb_conda_kernels |
This extension enables a Jupyter Notebook or JupyterLab application in one conda environment to access kernels for Python, R, and other languages found in other environments. When a kernel from an external environment is selected, the kernel conda environment is automatically activated before the kernel is launched. This allows you to utilize different versions of Python, R, and other languages from a single Jupyter installation.
The package works by defining a custom KernelSpecManager
that
scans the current set of conda
environments for kernel
specifications. It dynamically modifies each KernelSpec
so that it can be properly run from the notebook environment.
When you create a new notebook, these modified kernels
will be made available in the selection list.
This package is designed to be managed solely using conda
.
It should be installed in the environment from which
you run Jupyter Notebook or JupyterLab. This might be your base
conda
environment, but it need not be. For instance,
if the environment notebook_env
contains the notebook
package, then you would run
conda install -n notebook_env nb_conda_kernels
Any other environments you wish to access in your
notebooks must have an appropriate kernel
package installed. For instance, to access a Python
environment, it must have the ipykernel
package; e.g.
conda install -n python_env ipykernel
To utilize an R environment, it must have the r-irkernel
package; e.g.
conda install -n r_env r-irkernel
For other languages, their corresponding kernels must be installed.
This extension works out of the box only with Jupyter notebooks and JupyterLab.
A new kernel discovery system is being developed that should enable the wider Jupyter ecosystem to take advantage of these external kernels. This package will require modification to function properly in this new system.
But you can activate a workaround for it to work with
Jupyter Console, nbconvert
, and other tools. As
these tools were not designed to allow for the use of custom
KernelSpecs, you can set the configuration parameter kernelspec_path
to tell this extension to add dynamically the conda environment to
the kernel list. To set it up:
jupyter_config.json
in the folder returned by jupyter --config-dir
.{
"CondaKernelSpecManager": {
"kernelspec_path": "--user"
}
}
python -m nb_conda_kernels list
jupyter
:
jupyter kernelspec list
The previous command should list the same kernel than nb_conda_kernels
.
You are now all set. nbconvert
, voila
, papermill
,... should find the
conda environment kernels.
This package introduces two additional configuration options:
conda_only
: Whether to include only the kernels not visible from Jupyter normally or not (default: False except if kernelspec_path
is set)
env_filter
: Regex to filter environment path matching it. Default: None
(i.e. no filter)
kernelspec_path
: Path to install conda kernel specs to if not None
. Default: None
(i.e. don't install the conda environment as kernel specs for other Jupyter tools)
Possible values are:
""
(empty string): Install for all users--user
: Install for the current user instead of system-wide--sys-prefix
: Install to Python's sys.prefixPREFIX
: Specify an install prefix for the kernelspec. The kernel specs will be
written in PREFIX/share/jupyter/kernels
. Be careful that the PREFIX
may not be discoverable by Jupyter; set JUPYTER_DATA_DIR to force it or run
jupyter --paths
to get the list of data directories.name_format
: String name format
Default: '{language} [conda env:{environment}]'
Available field names within the string:
{0}
= Language{1}
= Environment name{conda_kernel}
= Dynamically built kernel name for conda environment{display_name}
= Kernel displayed name (as defined in the kernel spec){environment}
= Environment name (identical to {1}
){kernel}
= Original kernel name (name of the folder containing the kernel spec){language}
= Language (identical to {0}
)enable_debugger
: Override kernelspec debugger metadata
Default: None
Possible values are:
true
false
In order to pass a configuration option in the command line use python -m nb_conda_kernels list --CondaKernelSpecManager.env_filter="regex"
where regex is the regular expression for filtering envs "this|that|and|that" works.
To set it in jupyter config file, edit the jupyter configuration file (py or json) located in your jupyter --config-dir
jupyter_config.py
- add a line "c.CondaKernelSpecManager.env_filter = 'regex'"jupyter_config.json
- add a json key {
"CondaKernelSpecManager": {
"env_filter": "regex"
}
Install Anaconda or Miniconda. If you are on Windows, make sure you have a Bash shell on your path.
Create and activate the testbed environment by running
source testbed/build.sh
This performs the following steps:
../nbckdev
,
or in CONDA_ROOT
if that environment variable is defined.
(Note that the default directory ../nbckdev
is at the same
level as your copy of the repository. This is because we do
not want conda build
to try to capture the entire testbed
into the build workspace.)If the environment already exists, testbed/build.sh
will
quickly exit, so it is safe to run it if you are not sure.
Run pytest to test the package.
pytest tests
The root environment of our testbed uses Python 3.7. If you would
like to test nb_conda_kernels
with a different Python version,
create a new child environment:
conda create -n ptest python=... notebook pytest pytest-cov requests mock
conda install backports.functools_lru_cache # python 2 only
conda activate ptest
pip install -e .
python -m nb_conda_kernels.install --enable
pytest tests
conda info
subprocess call in manager.pyname_format
setting)nb_conda_kernels
with tools such as voila
, papermill
, nbconvert
env_filter
conda
from the PATHconda
package building