Odd behavior with Jupyter notebook and virtual environment

Viewed 65

I installed the anaconda python distribution on Ubuntu 16-04 (LTS release) and I want to use virtual environments with jupyter notebooks but I get some odd behavior:

conda update conda

conda create -n myvirtenv python=3.6 anaconda

conda activate myvirtenv

added virtual enviroment to jupyter

python -m ipykernel install --user –name=myvirtenv

When I start a jupyter notebook from my default enviroment and get the python version:

import sys

print(sys.version)
3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0]

That is as expected but when I go to Kernel > change kernel and select myvirtenv I get the same output.

When I activate myvirtenv and start jupyter notebook with default kernel I get the following output:

print(sys.version)
3.6.10 |Anaconda, Inc.| (default, Jan  7 2020, 21:14:29) 
[GCC 7.3.0]

When I change to kernel to myvirtenv I get the following output:

print(sys.version)
3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0]

I would expect that when I start jupyter notebook from either the default environment or myvirtenv select the kernel myvirtenv it would use python 3.6.10 and the default kernel would use python 3.7.4? What can I do to ensure that kernel myvirtenv uses the correct virtual environment?

1 Answers

I found it:

kernel.json file was pointing to wrong python:

{
 "argv": [
  "/home/****/anaconda3/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "myvirtenv",
 "language": "python"
}

Changed it to:

{
 "argv": [
  "/home/****/anaconda3/envs/myvirtenv/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "myvirtenv",
 "language": "python"
}
Related