Using a Kernel Based on a Conda Environment

Hi folks,
I wonder how to force my jupyter notebook to use a specific kernel based on a conda environment. For instance, I created a kernel using a specific requirements.yml file (which lists all the dependencies for this environment). Then I exported the environment to jupyter. This environment is supposed to use tensorflow 1.13. The version of tensorflow installed in my over all anaconda installation is version 2. However, when I check the version of tensorflow from my new notebook (using the kernal based on the environment that I created), it shows it to be 2.11. I wonder why is that? Any help would be really appreciated.

There are a few tools to manage this.

nb_conda_kernels exists, but hasn’t seen any attention from maintainers for a long time.

gator was originally a fork of the above, and has seen more development recently. It also conflates the concern of building and managing environments, a la nb_conda (also abandoned), and uses mamba, which is usually an order of magnitude faster than conda at solving and generally better at downloading as well.

However, the simplest mechanism is something akin to the docs, with the last example being the most self-contained, (and therefore scriptable):

/path/to/kernel/env/bin/python -m ipykernel install \
  --prefix /path/to/jupyter/env \
  --name python-my-env \
  --display-name "Python 3 (other-env)"
/path/to/kernel/env/bin/python -m jupyter kernelspec list

This avoids some of the complications in the other examples, such as intermediate /tmp files, the --user environment (which makes all kinds of things unpredictable).

(Reviving topic after 11 months (:
This post seems to be about the same issue I’m facing: running a notebook from within a conda venv.

[Jupyter Notebook 6.5.4]

I’d already tried what I’d found in this doc, and then tried what @bollwyvl’s post recommended above, without the --user option that “… makes all kinds of things unpredictable”!.

the notebook fires up and I can select the conda environment’s kernel. but when I attempt to import a module that has been installed into the conda env I get a Module not found error?

Here’s what kernelspec list reports:

python -m jupyter kernelspec list
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Available kernels:
python3 …/miniconda3/envs/ai4law/share/jupyter/kernels/python3
ai4law /Users/rik/Library/Jupyter/kernels/ai4law

(not sure what the Xfrozen_modules bit is about? I tried providing the suggested option to ipkernel but it was considered a bad config?)

I also tried using the python3 kernel (since it was bound to the conda env), but same behavior: unable to import module.

anyone have further ideas?