notebook icon indicating copy to clipboard operation
notebook copied to clipboard

Jupyter Notebook is loading incorrect Python kernel

Open rjtjs opened this issue 8 years ago • 41 comments
trafficstars

I am on OSX Sierra (10.12.5), and am using Jupyter Notebook v 4.3.0. I have installed both Python2 and Python3 using Homebrew, and both kernels for Jupyter Notebook. Upon opening the notebook to use Python3 kernel, I find that it still uses Python2.

I might be missing something obvious in my installation, but I can't seem to figure out what it is.

Detailed Description

Installation:

I installed both Python2 and Python3 using Homebrew, and Jupyter Notebook on my system thus: brew install python3 pip3 install --upgrade pip3 setuptools wheel pip3 install jupyter

I ran all analogous commands for Python2 as well.

Configuration:

I generated the config file using jupyter notebook --generate-config, and the only configurations I have "enabled" in my config file are: c.NotebookApp.browser = u'safari' c.NotebookApp.password = 'sha1:somepwd'

Both kernels installed:

Both kernels seem to be visible to kernelspec:

jupyter kernelspec list
Available kernels:
  python2    /usr/local/share/jupyter/kernels/python2
  python3    /usr/local/share/jupyter/kernels/python3

Both kernels are also available as options when I start execute jupyter notebook: screen shot 2017-06-08 at 1 49 24 pm

As an additional check, pip list and pip3 list both include ipykernel.

Problem:

When I opened a new notebook using Python3 kernel, none of the packages I installed using pip3 were recognized when I used import. Furthermore, sys.path returns path to Python2:

screen shot 2017-06-08 at 1 59 32 pm

When I run print "Hello World", it gives me a successful output (which is true in Python2, but should throw an error in Python3).

However, when I use the (depreciated) command ipython3 notebook, things work as expected: screen shot 2017-06-08 at 1 58 29 pm and all my Python3 packages are successfully recognized.

It doesn't seem to be the biggest deal breaker, as I can continue working with ipython3 notebook, but I wonder what I am missing that causes jupyter notebook to not load Python3.

rjtjs avatar Jun 08 '17 18:06 rjtjs

Can you look at the kernel.json in each of those kernelspec directories you found?

takluyver avatar Jun 09 '17 14:06 takluyver

kernel.json in python2 directory:

{
 "display_name": "Python 2", 
 "language": "python", 
 "argv": [
  "python", 
  "-m", 
  "ipykernel_launcher", 
  "-f", 
  "{connection_file}"
 ]
}

kernel.json in python3 directory:

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

rjtjs avatar Jun 09 '17 23:06 rjtjs

This looks similar to jupyter/jupyter#270 . Ping @minrk - I think this is due to installing kernelspecs automatically as data files in the wheel.

takluyver avatar Jun 12 '17 11:06 takluyver

For now, you can probably work around it by changing the first item in argv to 'python3' in the Python 3 kernel.json file.

takluyver avatar Jun 12 '17 11:06 takluyver

Yup, it's the same. Running ipython kernel install for Python 2 and 3 will resolve the ambiguity by installing with sys.executable's abspath that we can't do in data_files. While the new data_files work better in envs of all kinds and with just one Python, two Pythons running on the same prefix (e.g. --user on linux, but not mac) run into ambiguity.

minrk avatar Jun 12 '17 16:06 minrk

I didn't fully understand what @minrk said above, but running ipython kernel install --name python3 didn't solve the issue, if that helps.

As @takluyver suggested, changing argv solved it for me, thank you. I changed the first argument of argv in python3 folder's kernel.json from python2 path to python3 path, and everything works as expected.

rjtjs avatar Jun 12 '17 20:06 rjtjs

I got a similar problem

messages image 3819428338

I solved it by sudo pip install --upgrade ipykernel

chapter09 avatar Jul 28 '17 18:07 chapter09

See https://stackoverflow.com/a/37857536/5658349

pjpjq avatar Aug 03 '17 09:08 pjpjq

Solved this issue by executing

python3 -m ipykernel install --user

nvdv avatar Sep 19 '17 08:09 nvdv

I also am experiencing this issue, and editing the two kernel.json files has solved the problem. Will I have to do this every time I upgrade jupyter on my machine?

cbcoutinho avatar Jan 08 '18 10:01 cbcoutinho

Hopefully not. We've refined this behaviour in a new version of jupyter_client

takluyver avatar Jan 08 '18 11:01 takluyver

This is also happening to me. Using ipython3 notebook works.

actongorton avatar Jan 19 '18 22:01 actongorton

Solved this issue by executing

python3 -m ipykernel install --user

works like charm

mustafa-qamaruddin avatar Nov 26 '18 23:11 mustafa-qamaruddin

My jupyter notebook was using a python3 kernel even though I wanted it to use a python2 kernel (and the web interface was showing python2).

  1. Verify that the actual kernel is python3 by running the following from within the jupyter notebook:
import platform 
print(platform.python_version())
  1. Verify kernel is missing:

jupyter kernelspec list

  1. Install new kernel:

sudo -H pip2 install jupyter

  1. Restart jupyter notebook (repeat step 1 to confirm output is what's expected).

Olshansk avatar Dec 08 '18 23:12 Olshansk

This was my solution: https://github.com/jupyter/notebook/issues/2359#issuecomment-453205175

auwsom avatar Jan 10 '19 18:01 auwsom

works for me with python3 -m ipykernel install --user - THANK YOU nvdv 👍

Amarumi avatar Mar 07 '19 20:03 Amarumi

Changing the executable path of jupyter (in the kernel.json) worked in my case. For details see https://stackoverflow.com/a/57274684/3521099 .

rraadd88 avatar Jul 30 '19 15:07 rraadd88

I'm having a similar issue on Mac OS. When trying to run the kernel within a virtual environment, the system python (brew installed) is loaded instead.

When the virtualenv is active:

(venv) lubaroli@machine: ~$ which python
/Users/lubaroli/.env/venv/bin/python

Likewise:

(venv) lubaroli@machine: ~$ which IPython
/Users/lubaroli/.env/venv/bin/IPython

When running: python kernel install --user --name=venv

The kernel.json file reads:

{
 "argv": [
  "/usr/local/opt/python/bin/python3.7",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "venv",
 "language": "python"
}

Changing the kernel.json path manually to the venv path does not fix the issue. When running a notebook, sys.executable still returns: '/usr/local/opt/python/bin/python3.7'

Any ideas?

lubaroli avatar Aug 26 '19 02:08 lubaroli

Solved mine with python3 -m ipykernel install --user

SimonOsipov avatar Aug 29 '19 13:08 SimonOsipov

as reported by others, sys.path and sys.executable agree from within the virtualenv (I'm using pipenv, btw), the latter correctly pointing to the virtualenv:

>>> sys.executable
'/Users/tullio/.local/share/virtualenvs/data_science-V36xIoci/bin/python'

but from within jupyter lab, sys.executable moves to pointing to

>>> sys.executable
'/usr/local/opt/python/bin/python3.7'

so far, 2 solutions have been proposed, neither of which worked for me:

Running python3 -m ipykernel install --user

did not fix the issue for me (I'm assuming it has to be run after activating the venv, and before opening jupyter lab, right?)

Editing the kernel.json file

Running

jupyter kernelspec list

returns me the same, single kernel.json file regardless of whether I do it from the virtualenv or from the jupyter lab instance I launch from within it. argv[0] really looks fine to me, no reason to change it:

$ more kernel.json
{
 "argv": [
  "/Users/tullio/.local/share/virtualenvs/data_science-V36xIoci/bin/python3",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

And seriously, jupyter: threads like this one have been going on for ages. I've installed jupyter lab from within the virtualenv - WHY ON EARTH would I want sys.executable to point to anywhere but to the same value that will be returned when launching python from the virtualenv?

tbagnoli avatar Sep 30 '19 08:09 tbagnoli

I opened /usr/local/share/jupyter/kernels/python2/kernel.json and rewrite the python path, but I couldn't save the file after that.

I am using a server instead of stand alone OS, just in case this is the reason, if it is like this, do you have any idea please?

Thidashwe avatar Jan 30 '20 08:01 Thidashwe

The inability to save edits to a file directly is more of a system thing. You'll need to look at the kernel.json's permissions and change users and/or permissions to complete the save operation. Since this is a server, you may need to enlist the help of your system's administrator.

kevin-bates avatar Jan 30 '20 15:01 kevin-bates

Instead of changing kernel under /usr/local/share/jupyter/kernels/python3/kernel.json, the problem solved by modifying the kernel under ~/.local/share/jupyter/kernels/python3/kernel.json into { "argv": [ "home/username/anaconda3/bin/python3", //username here refers to specific's user name "-m", "ipykernel_launcher", "-f", "{connection_file}" ], "display_name": "Python 3", "language" : "python" } Just if it could help anyone else who encounter the same problem as I do.

Thidashwe avatar Jan 31 '20 02:01 Thidashwe

solved my problem with python3 -m ipykernel install --user on macOS Catalina 10.15.3 with anaconda

aaaven avatar Mar 12 '20 03:03 aaaven

same issue here as tbagnoli and lubaroli using conda environments.

I generate the environment using the code below:

conda create --name venv python pip ipykernel
conda activate venv
pip install -e /path/to/local/package

Within terminal the environments agree:

~
venv ❯ which python
/Users/nicholasmi/opt/anaconda3/envs/venv/bin/python

~
venv ❯ which pip   
/Users/nicholasmi/opt/anaconda3/envs/venv/bin/pip

I then install the kernelspec via:

~
venv ❯ python -m ipykernel install --user --name venv --display-name "Python (venv)"

Using the kernelspec in Jupyter then yields: image

Does a solution exist? The methods above don't help. For example, the kernel.json file seems to be set up correctly already.

~/Library/Jupyter/kernels/venv 19s
base ❯ cat kernel.json      
{
 "argv": [
  "/Users/nicholasmi/opt/anaconda3/envs/venv/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python (venv)",
 "language": "python"
}```

nicholas-miles avatar Jul 17 '20 17:07 nicholas-miles

I just recently started to get the exact same behaviour as @nicholas-miles - glad it's not only me.

mathieuboudreau avatar Jul 20 '20 19:07 mathieuboudreau

Were you in the activated environment (e.g., venv) when you launched the notebook server?

kevin-bates avatar Jul 20 '20 20:07 kevin-bates

I use conda for my venvs, but yes.

And doing which jupyter confirmed that I was calling the jupyter executable that's in my conda venv path.

mathieuboudreau avatar Jul 20 '20 22:07 mathieuboudreau

Thanks @mathieuboudreau. I had seen that the kernel.json file was viewed from the base env in @nicholas-miles' example, so I thought I'd toss that out there.

When you say you recently started to get the exact same behavior, how recent are you talking about and were there any significant upgrades you may be aware of around that time as well?

kevin-bates avatar Jul 20 '20 23:07 kevin-bates

I was on vacation last week. If I recall, it was working correctly before then, and isn't working now. I don't think made any upgrades, but I could try and delete anaconda and reinstall it.

mathieuboudreau avatar Jul 21 '20 17:07 mathieuboudreau