Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jupyter Notebook is loading incorrect Python kernel #2563

Open
rjtjs opened this issue Jun 8, 2017 · 20 comments

Comments

@rjtjs
Copy link

@rjtjs rjtjs commented Jun 8, 2017

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.

@takluyver

This comment has been minimized.

Copy link
Member

@takluyver takluyver commented Jun 9, 2017

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

@rjtjs

This comment has been minimized.

Copy link
Author

@rjtjs rjtjs commented Jun 9, 2017

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"
}
@takluyver

This comment has been minimized.

Copy link
Member

@takluyver takluyver commented Jun 12, 2017

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

This comment has been minimized.

Copy link
Member

@takluyver takluyver commented Jun 12, 2017

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

@minrk

This comment has been minimized.

Copy link
Member

@minrk minrk commented Jun 12, 2017

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.

@rjtjs

This comment has been minimized.

Copy link
Author

@rjtjs rjtjs commented Jun 12, 2017

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.

@chapter09

This comment has been minimized.

Copy link

@chapter09 chapter09 commented Jul 28, 2017

I got a similar problem

messages image 3819428338

I solved it by sudo pip install --upgrade ipykernel

@wendingp

This comment has been minimized.

Copy link

@wendingp wendingp commented Aug 3, 2017

@nvdv

This comment has been minimized.

Copy link
Contributor

@nvdv nvdv commented Sep 19, 2017

Solved this issue by executing

python3 -m ipykernel install --user
@cbcoutinho

This comment has been minimized.

Copy link

@cbcoutinho cbcoutinho commented Jan 8, 2018

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?

@takluyver

This comment has been minimized.

Copy link
Member

@takluyver takluyver commented Jan 8, 2018

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

@actongorton

This comment has been minimized.

Copy link

@actongorton actongorton commented Jan 19, 2018

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

@mustafa-qamaruddin

This comment has been minimized.

Copy link

@mustafa-qamaruddin mustafa-qamaruddin commented Nov 26, 2018

Solved this issue by executing

python3 -m ipykernel install --user

works like charm

@Olshansk

This comment has been minimized.

Copy link

@Olshansk Olshansk commented Dec 8, 2018

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).
@auwsom

This comment has been minimized.

Copy link

@auwsom auwsom commented Jan 10, 2019

This was my solution:
#2359 (comment)

@Amarumi

This comment has been minimized.

Copy link

@Amarumi Amarumi commented Mar 7, 2019

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

@rraadd88

This comment has been minimized.

Copy link

@rraadd88 rraadd88 commented Jul 30, 2019

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

@lubaroli

This comment has been minimized.

Copy link

@lubaroli lubaroli commented Aug 26, 2019

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?

@SimonOsipov

This comment has been minimized.

Copy link

@SimonOsipov SimonOsipov commented Aug 29, 2019

Solved mine with python3 -m ipykernel install --user

@tbagnoli

This comment has been minimized.

Copy link

@tbagnoli tbagnoli commented Sep 30, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.