2

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.

yatu
  • 86,083
  • 12
  • 84
  • 139
  • If Jupyter is installed through Conda, e.g., in your **base**, then I find the cleaner workflow is not to manually register envs, but instead install `nb_conda_kernels` in the environment with `jupyter` and it will automatically discover any environments with appropriate kernel packages installed (e.g., `ipykernel`, `r-irkernel`). See [this related answer](https://stackoverflow.com/a/43197286/570918). – merv Dec 01 '20 at 14:55

1 Answers1

1

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.

yatu
  • 86,083
  • 12
  • 84
  • 139
Swishonary
  • 93
  • 1
  • 6