Warm tip: This article is reproduced from serverfault.com, please click

Jupyter notebook kernel different from conda environment

发布于 2020-12-01 11:44:50

I'm encountering problems while trying to reproduce a conda environment in a jupyter notebook kernel. I've created a conda environment, mlflow, and by activating it, I can import mlflow, as you can see below:

[ 12:36:18 ] ~ base ❯ conda activate mlflow
[ 12:36:21 ] ~ mlflow ❯ python
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mlflow
>>>

Then I proceed to install the kernel in jupyter, as:

python -m ipykernel install --name mlflow

However, once in the mlflow kernel in jupyter, I cannot import the same module, mlflow. Why could that be? It suspect that the issue is that C:\Users\userx\AppData\Roaming\jupyter\kernels\mlflow\kernel.json is pointing to the wrong python:

{
 "argv": [
  "C:\\Users\\userx\\Anaconda3\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "mlflow",
 "language": "python"
}

Whereas it should be pointing to the python in the mlflow environment. However, I tried to change it and the caused the notebook to fail when launching.

More details:

[ 12:39:27 ] ~ base ❯ jupyter --path
config:
    C:\Users\userx\.jupyter
    C:\Users\userx\Anaconda3\etc\jupyter
    C:\ProgramData\jupyter
data:
    C:\Users\userx\AppData\Roaming\jupyter
    C:\Users\userx\Anaconda3\share\jupyter
    C:\ProgramData\jupyter
runtime:
    C:\Users\userx\AppData\Roaming\jupyter\runtime

EDIT

It seems to work if I install jupyter in the environment, and launch a notebook from the environment. However, it'd still be nice to know how to do the same using the jupyter in base.

Questioner
yatu
Viewed
0
74.4k 2020-12-01 20:36:06

Try giving the full conda python path and see if that resolves it:

C:\Users\<username>\anaconda\envs\<environment-name>\bin\python -m ipykernel install --name mlflow

The reason ipykernel was not found was because it was not installed in the right python environment. Using "python -m" may reference the native python outside conda or maybe the python in the base environment. Specifying the full path just ensures that it gets installed in the right environment.