notebook: Jupyter Notebook is loading incorrect Python kernel
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:

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:
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:
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.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 19
- Comments: 43 (12 by maintainers)
Solved this issue by executing
I got a similar problem
I solved it by
sudo pip install --upgrade ipykernelFor now, you can probably work around it by changing the first item in
argvto ‘python3’ in the Python 3 kernel.json file.as reported by others,
sys.pathandsys.executableagree from within the virtualenv (I’m using pipenv, btw), the latter correctly pointing to the virtualenv:but from within jupyter lab,
sys.executablemoves to pointing toso far, 2 solutions have been proposed, neither of which worked for me:
Running
python3 -m ipykernel install --userdid 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.jsonfileRunning
returns me the same, single
kernel.jsonfile 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: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.executableto point to anywhere but to the same value that will be returned when launching python from the virtualenv?This is also happening to me. Using
ipython3 notebookworks.Yup, it’s the same. Running
ipython kernel installfor 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.The problem with
which pythoninside notebook not pointing to the same assys.executableis caused by a difference in system paths. In order to make sure they are pointing to the same executable:python -m ...--env PATH=...For my setup with virtualenv it looks like this
In general:
Solved mine with
python3 -m ipykernel install --userI’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:
Likewise:
When running:
python kernel install --user --name=venvThe kernel.json file reads:
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?
works for me with python3 -m ipykernel install --user - THANK YOU nvdv 👍
works like charm
solved my problem with
python3 -m ipykernel install --useron macOS Catalina 10.15.3 with anacondaMy jupyter notebook was using a python3 kernel even though I wanted it to use a python2 kernel (and the web interface was showing python2).
jupyter kernelspec listsudo -H pip2 install jupyterSee https://stackoverflow.com/a/37857536/5658349
It seems like the crux of the issue is that the correct python is in use by the kernel, but the PATH (from the kernel) doesn’t reflect the python in use. As a result, while python statements (e.g.,
import foo) produce the desire results, any shell commands (e.g.,!pip install foo) do not. In such cases, you could fully-qualify the shell command and, I believe, get the desire results.I found the IPython’s documentation link in the Python Environments section of the troubleshooting documentation to be fairly helpful. In particular the portion about installing the kernel with the
--prefixoption to point at an env different that the current env.Jupyter, by default, doesn’t activate environments for a given kernel. This is why the PATH-based commands (sans prefixing) can be problematic. If you find that activation may resolve things for you, then perhaps taking a look at nb_conda_kernels may be helpful. This package updates your jupyter’s configuration to use an alternate
KernelSpecManagerthat is used for discovering kernels (which some find invasive). These kernel specifications contain information that, AFAIK, activate the environment associated with that kernel prior to its launch.Here’s a screenshot of a kernel I’ve started that uses a python from an environment other than the notebook server. The notebook server is launched in my activated
notebook-devenv, while the kernel I want to run is in myenterprise-gateway-devenv. Aside from the PATH and sys.executable difference, you’ll find that theimportstatement produces the desired result (my notebook-dev env doesn’t have kubernetes installed). In addition, my EG env doesn’t havenoseinstalled until I fully qualify thepipcommand to install it into the kernel’s env (i.e., enterprise-gateway-dev).I hope you find this helpful and apologize for the frustration.
same issue here as tbagnoli and lubaroli using conda environments.
I generate the environment using the code below:
Within terminal the environments agree:
I then install the kernelspec via:
Using the kernelspec in Jupyter then yields:
Does a solution exist? The methods above don’t help. For example, the
kernel.jsonfile seems to be set up correctly already.I didn’t fully understand what @minrk said above, but running
ipython kernel install --name python3didn’t solve the issue, if that helps.As @takluyver suggested, changing
argvsolved it for me, thank you. I changed the first argument ofargvinpython3folder’skernel.jsonfrompython2path topython3path, and everything works as expected.@nemusa Thank you so much, this worked for me! It was driving me crazy that I couldn’t figure out why the PATH inside the shell didn’t match my $PATH
@jianboy I solved it with this
You may need to restart the kernel
ipython kernel install --userworked for meHi @yasirroni - try using the
%pipmagic. This is exactly the problem it resolves.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.kernel.jsoninpython2directory:kernel.jsoninpython3directory: